你的位置:首页 > 软件开发 > Java > AngularJS自定义Directive不一定返回对象

AngularJS自定义Directive不一定返回对象

发布时间:2015-12-28 22:00:11
AngularJS中,当需要自定义Directive时,通常返回一个对象,就像如下的写法: angular.module(modulename) .directive(myDirective, function(){ return { restric ...

 

AngularJS中,当需要自定义Directive时,通常返回一个对象,就像如下的写法:

 

angular.module('modulename')  .directive('myDirective', function(){    return {      restrict: 'EA', //E表示element, A表示attribute,C表示class,M表示commnent,即注释      scope:{        title: '@' //@读属性值,=双向绑定,&用户函数      }      template: '<div>{{myVal}}</div>',      templateUrl: 'app/sample.html',      controller: 'myController',      link:function($scope, element, attrs){}//DOM操作    };  })
在页面中按如下调用:<div enter leave>I am content</div>

 

另外,link函数还有一个attrs形参用来描述所有的属性,通过"attrs.属性名"来获取属性值。

 

app.directive("enter", function(){  return function(scope, element, attrs){    element.bind("mouseenter", function(){      element.addClass(attrs.enter);    })  }});app.directive("leave", function(){  return function(scope, element,attrs){    element.bind("mouseleave", function(){      element.removeClass(attrs.enter);    })  }})
<div enter="panel" leave>I am content</div>


原标题:AngularJS自定义Directive不一定返回对象

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。