angularjs 指令的属性绑定方式有三种表示方式:@,=,&
1:先说指令域scope的@
我觉得描述很费劲,直接用代码来阐述:
<!doctypehtml> <htmlng-app='myApp'> <head> </head> <body> <divng-controller="listCtrl"> <inputtype="text"ng-model="t"/> <kidtitle="{{t}}">//这个必须指定的,这里的title是指令里scope的@对应的,t就是控制域scope下的 <span>我的angularjs</span> </kid> </div> <scripttype="text/javascript"src="angular.js"></script> <scripttype="text/javascript"src="main05.js"></script> </body></html>
main05.js
varmyApp=angular.module('myApp',[]); myApp.controller('listCtrl',function($scope){ $scope.logchore="motorola"; }); myApp.directive('kid',function(){ return{ 'restrict':'E',scope:{ title:"@" },template:'<div>{{title}}</div>' } }); 在输入框输入数字会绑定到指令模板的title中。
2:再说说Scope的 =
<!doctypehtml> <htmlng-app='myApp'> <head> </head> <body> <divng-controller="listCtrl"> <inputtype="text"ng-model="t"/> <kidtitle="t">//和上面相比,这个直接赋值等于scope域下的t了 <p>{{title}}</p> <span>我的angularjs</span> </kid> </div> <scripttype="text/javascript"src="angular.js"></script> <scripttype="text/javascript"src="main05.js"></script> </body></html>
main05.js代码如下:
varmyApp=angular.module('myApp',scope:{ title:"=" },template:'<div>{{title}}</div>' } });
<!doctypehtml> <htmlng-app='myApp'> <head> </head> <body> <divng-controller="listCtrl"> <kidflavor="logchore()"></kid>//先比=,函数赋值的形式,而logchore函数必须是域scope下声明的函数 </div> <scripttype="text/javascript"src="angular.js"></script> <scripttype="text/javascript"src="main05.js"></script> </body></html>
main05.js代码如下:
varmyApp=angular.module('myApp',function($scope){ $scope.logchore=function(){ alert('ok'); }; }); myApp.directive('kid',scope:{ flavor:"&" },template:'<div><buttonng-click="flavor()"></button></div>' } });
如果logchore带有参数,
<!doctypehtml> <htmlng-app='myApp'> <head> </head> <body> <divng-controller="listCtrl"> <kidflavor="logchore(t)"></kid> </div> <scripttype="text/javascript"src="angular.js"></script> <scripttype="text/javascript"src="main05.js"></script> </body></html>
main05.js代码如下:
varmyApp=angular.module('myApp',function($scope){ $scope.logchore=function(x){ alert(x); }; }); myApp.directive('kid',template:'<div><inputtype="text"ng-model="v"/><buttonng-click="flavor({t:v})"></button></div>' } });
原文地址:https://www.jb51.cc/angularjs/148010.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。