Augular JS 中向指令传递数据
@绑定策略的使用
<!doctype html>
<html ng-app="myApp">
<head>
<script src="./js/angular.js"></script>
</head>
<body>
<label>Their URL field:</label>
<input type="text" ng-model="theirUrl">
<div my-directive
some-attr="{{theirUrl}}"
my-link-text="Click me to go to Google"></div>
<script>
angular.module('myApp', [])
.directive('myDirective', function() {
return {
restrict: 'A',
replace: true,
scope: {
myUrl: '@someAttr',
myLinkText: '@'
},
template: '\
<div>\
<label>My Url Field:</label>\
<input type="text" ng-model="myUrl" />\
<a href="{{myUrl}}">{{myLinkText}}</a>\
</div>\
'
}
})
</script>
</body>
</html>
注:1.从此例中Their URL field中输入值会引起My Url Field输入框中值的改变,但在My Url Field中输入值不会引起Their URL field值改变。这表明绑定策略@把theirUrl的值复制给新作用域对象的myUrl属性。