简体   繁体   English

Angular2:通过指令将外部Component注入其他组件无效

[英]Angular2: Inject external Component into other component via directive not working

I want child component template to load into parent component template. 我希望子组件模板加载到父组件模板中。 (for the lack of better wording I call them child and parent) (由于缺乏更好的措辞,我称之为孩子和父母)

This is my 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() {

    }
}

This is my parent: 这是我的父母:

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

And the HTML of Parent: 和父母的HTML:

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

Any idea whats wrong? 有什么想法错了吗?

From the Angular 2 Cheat Sheet (search for selector) it says that by using brackets you are limiting where your directive can be called. Angular 2 Cheat Sheet (搜索选择器)中可以看出,通过使用括号,您可以限制调用指令的位置。 For example: 例如:

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

So if you take off the [] all should be well 所以如果你脱掉[]一切都应该好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM