[AngularJS] Compile(), element(), link(), watch()
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>