gpt4 book ai didi

javascript - Angular 需要带有 transclude 的父 Controller 不起作用

转载 作者:行者123 更新时间:2023-11-30 16:31:27 25 4
gpt4 key购买 nike

我试图从子指令中获取父 Controller ,但它抛出了这个错误:

Controller 'SelectHandlerController', required by directive 'inputControl', can't be found!

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>

问题是什么?

最佳答案

您的require 应该是您需要的指令的名称。不是 Controller 。

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);
}
}
});

关于javascript - Angular 需要带有 transclude 的父 Controller 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com