Git Product home page Git Product logo

Comments (2)

Wscats avatar Wscats commented on June 11, 2024

自定义过滤器,根据输入的关键词,全文搜索,如果有对应的结果那就让它变成红色背景

<!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.

Wscats avatar Wscats commented on June 11, 2024

AngularJS权威教程 第7章 过滤器filters

from angular-tutorial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.