gpt4 book ai didi

angular2-directives - 如何调用 Angular 2 指令中定义的方法

转载 作者:行者123 更新时间:2023-12-04 08:08:41 24 4
gpt4 key购买 nike

我是 Angular 2 的新手,我在当前组件中注入(inject)了指令

@Component({
selector: 'select-me',
templateUrl: 'app/template.html',
directives: [BackgroundChange] // I want to access this same instance to make the dynamic changes
})

export class PageComponent {
constructor(private backgroundChange:BackgroundChange) {
// I guess this will create new instance of BackgroundChange
}

}

我想调用 BackgroundChange 中可用的方法来动态地从 PageComponent 进行一些更改

最佳答案

指令是独立的,应该在您的 HTML 模板中使用 - 而不是注入(inject)到您的 PageComponent 中...也就是指令不可注入(inject)。

BackgroundChange 指令可以访问它附加到模板中的 HTML 元素 - 并且应该在其中进行操作。

角度文档:https://angular.io/docs/ts/latest/guide/attribute-directives.html#!#apply-directive

关于angular2-directives - 如何调用 Angular 2 指令中定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39107637/

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