我试图从子指令中获取父控制器,并抛出此错误:
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>
有什么问题?
答案 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);
}
}
});