Angular要求父控制器转换不起作用

时间:2015-10-21 19:03:49

标签: javascript angularjs

我试图从子指令中获取父控制器,并抛出此错误:

  

Controller' SelectHandlerController',指令所需   ' inputControl',无法找到!

app.controller('SelectHandlerController', function() {  
  this.model = '';
});

app.directive('selectHandler', () => {

  return {
    controller: 'SelectHandlerController',
    controllerAs: 'shc',
    transclude: true,
    template: '<div ng-transclude></div>',
    bindToController: true
  }


});


app.directive('inputControl', () => {


  return {
    template: `<div><input type="text"></div>`,
    require: '^SelectHandlerController',
    scope: {},
    bindToController: true,
    controllerAs: 'ic',
    link: function(scope, element, attrs, ctrl) {
      console.log(ctrl);
    }
  }

});

<select-handler>

  <input-control ></input-control> 

</select-handler>

有什么问题?

1 个答案:

答案 0 :(得分:2)

您的require应该是您要求的指令的名称。不是控制器。

app.directive('inputControl', function() {
  return {
    template: `<div><input type="text"></div>`,
    require: '^selectHandler',
    scope: {},
    bindToController: true,
    controllerAs: 'ic',
    link: function(scope, element, attrs, ctrl) {
      console.log(ctrl);
    }
  }
});