【发布时间】:2014-04-07 18:11:09
【问题描述】:
将控制器添加到元素指令时,例如:
.directive('hello', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div class="hello" ng-transclude></div>'
};
});
我无法访问控制器的范围:
.controller('HelloCtrl', function($scope) {
$scope.hello = "Hello World";
});
<hello ng-controller="HelloCtrl">
<h1>Hello Directive</h1>
<p>{{ hello }}</p>
</hello>
在这种情况下,{{ hello }} 未定义。该指令不会创建子范围或孤立范围。我还尝试使用{{ $parent.hello }} 访问该属性。
这里发生了什么?
我创建了一个 CodePen 来演示这种行为:http://codepen.io/jviotti/pen/ktpbE
【问题讨论】:
标签: angularjs angularjs-directive