【发布时间】:2016-07-20 15:04:20
【问题描述】:
我有一个组件,其模板如下所示:
<div [my-custom-directive]>Some content here</div>
我需要访问此处使用的MyCustomDirective 类实例。当我想访问子组件时,我使用ViewChild 查询。
是否有等效的功能可以访问子指令?
【问题讨论】:
标签: angular angular2-directives angular-template
我有一个组件,其模板如下所示:
<div [my-custom-directive]>Some content here</div>
我需要访问此处使用的MyCustomDirective 类实例。当我想访问子组件时,我使用ViewChild 查询。
是否有等效的功能可以访问子指令?
【问题讨论】:
标签: angular angular2-directives angular-template
您可以使用@Directive 注释的exportAs 属性。它导出要在父视图中使用的指令。在父视图中,您可以将其绑定到视图变量并使用 @ViewChild() 从父类访问它。
以plunker 为例:
@Directive({
selector:'[my-custom-directive]',
exportAs:'customdirective' //the name of the variable to access the directive
})
class MyCustomDirective{
logSomething(text){
console.log('from custom directive:', text);
}
}
@Component({
selector: 'my-app',
directives:[MyCustomDirective],
template: `
<h1>My First Angular 2 App</h1>
<div #cdire=customdirective my-custom-directive>Some content here</div>
`
})
export class AppComponent{
@ViewChild('cdire') element;
ngAfterViewInit(){
this.element.logSomething('text from AppComponent');
}
}
更新
如 cmets 中所述,上述方法还有另一种替代方法。
不使用exportAs,可以直接使用@ViewChild(MyCustomDirective)或@ViewChildren(MyCustomDirective)
这里有一些代码来演示三种方法之间的区别:
@Component({
selector: 'my-app',
directives:[MyCustomDirective],
template: `
<h1>My First Angular 2 App</h1>
<div my-custom-directive>First</div>
<div #cdire=customdirective my-custom-directive>Second</div>
<div my-custom-directive>Third</div>
`
})
export class AppComponent{
@ViewChild('cdire') secondMyCustomDirective; // Second
@ViewChildren(MyCustomDirective) allMyCustomDirectives; //['First','Second','Third']
@ViewChild(MyCustomDirective) firstMyCustomDirective; // First
}
更新
【讨论】:
cdire直接像@ViewChild(MyCustomDirective) element:MyCustomDirective;那样,在ngAfterViewInit - this.element.logSomething('text from...')。那么为什么你的方法直接通过你可以这样做的类型呢?只是为了澄清。
MyCustomDirective。如果有多个指令,它将匹配第一个。但是,当使用exportAs 时,您可以特别指定任何一个,例如第二个MyCustomDirective。
@ViewChildren()
@ViewChild('cdire', {read:MyCustomDirective}) secondMyCustomDirective: MyCustomDirective 和 <div #cdire my-custom-directive>Second</div>(无需导出)。
似乎自从@Abdulrahman 的回答以来,指令不能再从@ViewChild 或@ViewChildren 访问,因为它们只传递DOM 元素本身的项目。
相反,您必须使用@ContentChild/@ContentChildren 访问指令。
@Component({
selector: 'my-app',
template: `
<h1>My First Angular 2 App</h1>
<div my-custom-directive>First</div>
<div #cdire=customdirective my-custom-directive>Second</div>
<div my-custom-directive>Third</div>
`
})
export class AppComponent{
@ContentChild('cdire') secondMyCustomDirective; // Second
@ContentChildren(MyCustomDirective) allMyCustomDirectives; //['First','Second','Third']
@ContentChild(MyCustomDirective) firstMyCustomDirective; // First
}
@Component 属性上也不再有 directives 属性。
【讨论】:
this.firstMyCustomDirective 是undefined
正如其他答案的 cmets 中所述,这些其他(以前有效的)方法不适用于更新版本的 Angular。
不过,庆幸的是,有一种更简单的方法可以将其注入:直接从构造函数中注入!
@Component({
// ...
})
export class MyComponent implements OnInit {
// Would be *undefined*
// @ContentChild(MyDirective, { static: true })
// private directive: MyDirective;
constructor(private directive: MyDirective) { }
ngOnInit(): void {
assert.notEqual(this.directive, null); // it passes!
}
}
【讨论】:
Self() 是正确的方法,并且该值在构造函数中可用。 (womm)