gpt4 book ai didi

javascript - 在 Angular 分量中使用 'require'

转载 作者:可可西里 更新时间:2023-11-01 01:40:47 25 4
gpt4 key购买 nike

根据 the docs (具体来说,表格将指令与组件进行比较), Angular 组件允许需要其他指令(或者它只是组件?)。但是,组件没有链接功能,可以访问所需的 Controller 。 The source ,与文档相反,似乎暗示在创建组件时不可能使用“require”。哪个是真的?

最佳答案

引用的来源已过时。从 1.5.0 开始,组件 Controller can be required在其他组件中(同样适用于指令)。

指南中的示例 shows the way how the components and directives should interact在 1.5 中没有 link 的帮助。

require object and bindToController一起使用时,所需的 Controller 实例作为属性分配给当前 Controller 。

因为这发生在指令链接期间,所以所需的 Controller 在 Controller 构造函数中不可用,这就是为什么 $onInit magic method在那儿。如果存在,it is executed right after adding required controllers这个

两者

app.directive('someDirective', function () {
return {
scope: {},
bindToController: {},
controllerAs: 'someDirective',
require: {
anotherDirective: '^anotherDirective'
},
controller: function ($scope) {
console.log("You don't see me", this.anotherDirective);

this.$onInit = function () {
console.log("Now you do", this.anotherDirective);
};
}
}
});

app.component('someComponent', {
controllerAs: 'someComponent',
require: {
anotherDirective: '^anotherDirective'
},
controller: function ($scope) {
console.log("You don't see me", this.anotherDirective);

this.$onInit = function () {
console.log("Now you do", this.anotherDirective);
};
}
});

声明样式在引擎盖下是同等的,可以在 1.5 中互换使用,component 是一个简洁的样式。

关于javascript - 在 Angular 分量中使用 'require',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293680/

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