【发布时间】:2014-05-21 10:39:26
【问题描述】:
我有以下场景:具有隔离范围的 transclude 指令,其中附加了一个控制器:
angular.module('app', [])
.directive('hello', function() {
return {
controller: function($scope) {
$scope.hello = "Hello World";
},
restrict: 'E',
replace: true,
transclude: true,
template: '<div class="hello" ng-transclude></div>',
scope: {}
};
});
我期待从被嵌入的元素中访问指令的控制器:
<hello>
<h1>Hello Directive</h1>
<p>{{ hello }}</p>
</hello>
然而这似乎是不可能的。我尝试使用$parent 和$$nextSibling 访问hello。
这种情况有什么解决方案吗?我无法将控制器放在指令实例周围的包装器中。
我创建了一个代码笔来演示这种行为:http://codepen.io/jviotti/pen/ktpbE?editors=101
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope