HTML:
<!doctype html>
<html>
<head>
</head>
<body>
<div ng-app="project">
<div ng-controller="mainCtrl">
{{ property1 }}
<br />
{{ property2 }}
<div class="ts" d-child property1="{{ property1 }}cloud" property2="property2">
property1: {{ property1 }}
<br />
property2: {{ property2 }}
</div>
</div>
</div>
</body>
</html>
JS:
angular.module('project', [])
.controller('mainCtrl', function($scope) {
$scope.property1 = 'ss';
$scope.property2 = 'dd';
});
angular.module('project')
.directive('dChild', function() {
return {
restrict: 'A',
scope: {
property1: '@',
property2: '='
},
link: function(scope, element, attrs) {
}
// template: '<input type="text" ng-model="property1" />'
}
})
我认为property1: {{ property1 }}
将是“property1:sscloud”,但事实证明它是“ss”,好像它仍然指的是mainCtrl
控制器的范围,不应该请参考d-child
指令的范围?
如果我在指令中使用模板,它确实引用了正确的范围并显示'sscloud',任何人都可以告诉我为什么?
答案 0 :(得分:5)
angular.module('project')
.directive('dChild', function() {
return {
restrict: 'A',
transclude: true,
scope: {
property1: '@',
property2: '='
},
link: function(scope, element, attrs, ctrl, linker) {
linker(scope, function(clone, scope){
element.append(clone)
})
}
}
})
答案 1 :(得分:0)
我不太确定这一点,我很确定这一切都与评估每个{{}}时以及指令的范围变得孤立有关。我的建议是将内容放在模板中,因为它似乎在这样做时起作用。
如果你想在指令范围内阅读更多关于“@”和“=”的区别,这里是我发现的最好的文字。
What is the difference between '@' and '=' in directive scope in AngularJS?
答案 2 :(得分:0)
我认为您必须使用transclude
option
。
事实上,正如AngularJS
文档所说:
What does this transclude option do, exactly? transclude makes the contents of
a directive with this option have access to the scope outside of the directive
rather than inside.
由于您创建的Directives
已隔离 scope
更多文档: