我们实施了下一个指令:
angular.module('app', [])
.directive('dIsolatedWorks', function() {
return {
scope: {
prop: '='
},
template: '<span>{{name}}: {{prop}}</span>',
link: function(scope) {
scope.name = 'isolated';
scope.prop = 'link';
}
};
})
.directive('dIsolated', function() {
return {
scope: {
prop: '@'
},
template: '<span>{{name}}: {{prop}}</span>',
controller: function($scope) {
$scope.prop = 'controller';
},
link: function(scope) {
scope.name = 'isolated';
scope.prop = 'link';
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div d-isolated-works prop="attribute"></div>
<div d-isolated prop="attribute"></div>
</div>
实际上,在实施过程中,我确信对scope.prop
字段的赋值会改变变量,它会显示为'link'
,而不是'attribute'
。但目前我们看到真正的价值将是isolated: attribute
. 但是,可以通过将字符串分配更改为对象分配来简单地修复它。
你能解释一下这种行为吗?