Angular2:通过指令将外部组件注入其他组件无效
Angular2: Inject external Component into other component via directive not working
我希望 child 组件模板加载到 parent 组件模板中。 (由于缺乏更好的措辞,我称它们为 child 和 parent)
这是我的 child:
import {Component,Directive, ElementRef, Input} from '@angular/core';
import {IONIC_DIRECTIVES} from 'ionic-angular';
@Component({
selector: '[parentselector]',
template: '<div>I AM A CHILD</div>',
directives: [IONIC_DIRECTIVES]
})
export class ChildPage {
constructor() {
}
}
这是我的 parent:
@Component({
templateUrl: 'build/pages/parent/parent.html',
directives: [ChildPage]
})
和Parent的HTML:
<ion-content>
<ion-slides>
<ion-slide><parentselector>Parent To Be Overriden</parentselector>
</ion-slide>
</ion-slides>
</ion-content>
知道哪里出了问题吗?
在 Angular 2 Cheat Sheet(搜索选择器)中,它表示通过使用括号可以限制可以调用指令的位置。例如:
selector:'[parentselector]' - means you can only select as an attribute
selector:'parentselector' - means you can only select as an element
selector:'.parentselector' - means you can only select as a css class
所以如果你脱掉 []
一切都应该很好
我希望 child 组件模板加载到 parent 组件模板中。 (由于缺乏更好的措辞,我称它们为 child 和 parent)
这是我的 child:
import {Component,Directive, ElementRef, Input} from '@angular/core';
import {IONIC_DIRECTIVES} from 'ionic-angular';
@Component({
selector: '[parentselector]',
template: '<div>I AM A CHILD</div>',
directives: [IONIC_DIRECTIVES]
})
export class ChildPage {
constructor() {
}
}
这是我的 parent:
@Component({
templateUrl: 'build/pages/parent/parent.html',
directives: [ChildPage]
})
和Parent的HTML:
<ion-content>
<ion-slides>
<ion-slide><parentselector>Parent To Be Overriden</parentselector>
</ion-slide>
</ion-slides>
</ion-content>
知道哪里出了问题吗?
在 Angular 2 Cheat Sheet(搜索选择器)中,它表示通过使用括号可以限制可以调用指令的位置。例如:
selector:'[parentselector]' - means you can only select as an attribute
selector:'parentselector' - means you can only select as an element
selector:'.parentselector' - means you can only select as a css class
所以如果你脱掉 []
一切都应该很好