2016-07-07 48 views
3

我想要子組件模板加載到父組件模板中。 (爲缺乏更好的字眼我打電話給他們的孩子和父母)Angular2:通過指令不工作將外部組件注入其他組件

這是我的孩子:

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() { 

    } 
} 

這是我的父母:

@Component({ 
    templateUrl: 'build/pages/parent/parent.html', 
    directives: [ChildPage] 
}) 

和家長的HTML:

<ion-content> 
    <ion-slides> 
     <ion-slide><parentselector>Parent To Be Overriden</parentselector> 
     </ion-slide> 
    </ion-slides> 
</ion-content> 

任何想法最新怎麼了?

回答

7

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 

所以,如果你脫下[]都應該很好

+0

非常好!謝啦! – nottinhill