加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS 指令中的属性的绑定方式

发布时间:2020-12-17 09:35:50 所属栏目:安全 来源:网络整理
导读:angularjs 指令的属性绑定方式有三种表示方式:@,=, 1:先说指令域scope的@ 我觉得描述很费劲,直接用代码来阐述: !doctypehtmlhtmlng-app='myApp'head/headbodydivng-controller="listCtrl"inputtype="text"ng-model="t"/kidtitle="{{t}}"//这个必须指定

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>'

}
});

3:最后说&,这个是用来方法调用的

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

}
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读