Comments (2)
自定义过滤器,根据输入的关键词,全文搜索,如果有对应的结果那就让它变成红色背景
<!DOCTYPE html>
<html ng-app="wsscat">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<script src="angular.js"></script>
<body ng-controller="filterCtrl">
<input ng-model="red" ng-keyup="makeRed()" />
<div ng-bind-html="htmlSced"></div>
</body>
<script>
var app = angular.module('wsscat', []);
app.controller('filterCtrl', function($scope, $filter, $sce) {
var html = "<p>123</p>";
//刚进入时候显示的DOM结构
$scope.htmlSced = $sce.trustAsHtml(html)
$scope.makeRed = function() {
$scope.html = $filter("wsscat")(html, $scope.red);
$scope.htmlSced = $sce.trustAsHtml($scope.html);
//如果没有任何输入就变成原来的DOM结构
if(!$scope.htmlSced) {
$scope.htmlSced = $sce.trustAsHtml(html)
}
}
})
app.filter("wsscat", function() {
var func = function(input, val) {
//判断筛选结果是否为空
if(val) {
var output;
//把字符串<p>123</p>根据1的位置,拆分成["<p>", "23</p>"]
str = input.split(val);
//把1添加红色的背景色
var addRed = '<span style="background-color:red">' + val + '</span>';
//将1插回DOM中,生成一个新的DOM结构
output = str.join(addRed)
return output;
}
}
return func;
})
</script>
</html>
from angular-tutorial.
from angular-tutorial.
Related Issues (20)
- angular cdn
- ui-route和ng-route HOT 3
- $cacheFactory
- ionic总结 HOT 1
- ng-cookie
- ng-repeat绑定事件和嵌套
- ng-repeat嵌套
- service demo HOT 2
- Angular API文档
- directive demo HOT 1
- angular面试题 HOT 3
- AngularJS开发人员最常犯的10个错误
- 求指教阿!! HOT 1
- Angular中的MVC模型
- Angular的post请求后台接受不了数据的解决方法
- angular2
- 引入多个angular.js时候,angular程序会出错
- Angular指令(directive)中的controller,compile,link函数有什么不同?
- angular4
- angular4+webpack
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-tutorial.