我遇到了同样的问题,我的解决方法是观察嵌套指令中调用了哪些控制器。
# Parent Controller
app.controller 'storeController', ($scope, products) ->
$scope.cart = ["chicken", "pizza"]
$scope.addToCart = (item) ->
$scope.cart.push item
# from service
products.get().then (items) ->
$scope.products = items
# Parent Directives
app.directive 'storeContainer', ($scope, config) ->
restrict: 'E'
templatUrl: 'store-container.html'
controller: 'storeController'
# Nested Directive
app.directive 'storeFront', ($scope, config) ->
restrict: 'E'
templatUrl: 'store-front.html'
controller: 'storeController'
# Parent Template templates/directives/store-container.html
<div ng-repeat="item in cart">{{ item }}</div>
<strore-front></store-front>
# Nested Template templates/directives/store-front.html
<ul ng-repeat="item in products">
<li ng-click"addToCart(item)">{{ item }}</li>
</ul>
这里的错误是嵌套指令在原型链中创建了第二个控制器(storeController 的副本),父模板无权访问该控制器。要解决这样编写嵌套控制器的问题:
# Nested Directive
app.directive 'storeFront', ($scope, config) ->
restrict: 'E'
templatUrl: 'store-front.html'
有更好的方法来创建继承链,但这将解决许多学习 AngularJS 的人的问题。