如何在Angular 2 RC6中使用自定义指令?

时间:2016-09-17 19:05:29

标签: angular

我遇到了麻烦,包括自定义组件(指令)到我的角度组件中。

组件渲染,我也可以在DOM(Chrome开发工具)中看到该指令。但是,尽管我提供了一个模板,但为空

home.module.ts

import {NgModule, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import { HomeComponent } from './index';
@NgModule({
    declarations: [
        HomeComponent
    ],
    exports: [
        HomeComponent
    ],
    schemas: [
        CUSTOM_ELEMENTS_SCHEMA
    ]
})
export class HomeModule {
}

预输入-custom.component.ts

import {Component} from '@angular/core';

@Component({
    selector: 'typeahead-custom',
    template: `<div>
    <span>component</span>
<div style="padding-left: 5px">
    </div></div>
    `,
    styleUrls: [
        'app/includes/typeahead-custom/typeahead-custom.css'
    ]
})

export class TypeAheadCustomComponent {

    constructor () { }
}

home.component.ts

import { Component } from '@angular/core';

@Component({
    selector: 'as-home',
    templateUrl: 'app/home/home.html',
    styleUrls: [
        'app/home/home.css'
    ]
})

export class HomeComponent {
    public myData: any;
    public mySource: any[] = [
        { key:1, name:'Key One' },
        { key:2, name:'Key Two' },
        { key:3, name:'Key Three' },
        { key:4, name:'Key Four' }
    ];

    constructor() { }
}

DOM结果:

.....
<as-home _nghost-btf-4=""><typeahead-custom _ngcontent-btf-4=""></typeahead-custom>
</as-home>
....

1 个答案:

答案 0 :(得分:1)

import {TypeAheadCustomComponent } from './typeahead-custom.component';

@NgModule({
    declarations: [
        HomeComponent,TypeAheadCustomComponent   <///----here
    ],
    exports: [
        HomeComponent
    ],
    schemas: [
        CUSTOM_ELEMENTS_SCHEMA
    ]
})