Parent directive controller undefined when passing to child directive

前端 未结 2 956
迷失自我
迷失自我 2021-01-28 09:02

I asked general question here in this post. I\'ve got answer with working example; however when I try to use this example to modify existing code, I get error. See my code below

2条回答
  •  庸人自扰
    2021-01-28 09:48

    Because it's require not required.

    angular.module('myApp', [])
      .controller('MyDirectiveController', MyDirectiveController)
      .directive('tmpMenu', function() {
        return {
          restrict: 'AE',
          replace: true,
          transclude: true,
          scope: {
            disabled: '=?ngDisabled'
          },
          controller: 'MyDirectiveController',
          template: '
    myDirective Disabled: {{ disabled }}
    ', link: function(scope, element, attrs) {} }; }) .directive('tmpMenuLink', function() { return { restrict: 'AE', replace: true, transclude: true, require: '^^tmpMenu', template: '
    childDirective disabled: {{ disabled }}
    ', link: function(scope, element, attrs, MyDirectiveController) { scope.disabled = MyDirectiveController.isDisabled(); } }; }) function MyDirectiveController($scope) { this.isDisabled = function() { return $scope.disabled; }; }
    
    

提交回复
热议问题