zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[AngularJS] Compile(), element(), link(), watch()

angularjs Element Link watch compile
2023-09-14 08:59:21 时间

Functionail:

When user type "password" in the input.

The div will toggle class to alert-box alert.

/**
 * Created by Answer1215 on 8/23/2014.
 */
var app = angular.module("callApp", []);

app.directive('dumpPassword',function(){

    var pwdEl = angular.element('<div>{{model.text}}</div>');
    this.link = function(scope){
        scope.$watch('model.text', function(value){
            if(value === "password"){
                pwdEl.toggleClass('alert-box alert');
            }
        })
    };
    return{
        restrict: 'E',
        template: '<div> <input type="text" ng-model="model.text"></div>',
        replace:true,
        compile: function(elFromTemplate){
            elFromTemplate.append( pwdEl);
            return link;
        }
    }
});

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Angular Scope &</title>
    <link href="foundation.min.css" rel="stylesheet"/>
    <script src="angular.min.js"></script>
    <script src="app.js"></script>
</head>
<body ng-app="callApp">
    <dump-password></dump-password>
</body>
</html>