将数据传递给组件的指令

时间:2018-02-28 15:06:20

标签: angular input components directive

我有以下指令代码:

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';

@Directive({ selector: '[while]' })
export class WhileDirective {

constructor(private templateRef: TemplateRef<any>,
            private viewContainer: ViewContainerRef)
{ }

@Input() amountOfTeams: number;
@Input() counter:number;

@Input() set while(condition: boolean) {
        some logic....

    }
}

我的组件

import {Component, OnInit} from "@angular/core";
import {Game} from "../game";
import {PlayGameService} from "../playGame.service";


@Component({
    selector: "teams-Name",
    styleUrls:["teamsName.component.scss"],
    template: `<div  class='start-menu'>
<p  *while="" [amountOfTeams]="" [counter]=""></p>
</div>`

})

export class TeamsNameComponent {

    game: Game;
    counter:number = 0;

    constructor (public playGameService: PlayGameService) {}


    ngOnInit(){
        this.game = this.playGameService.getGame();
        console.log(this.game);
    }

}

我在app.module.ts

的声明中加载了WhileDirective

不幸的是我在编译时遇到错误:

  

未捕获错误:模板解析错误:无法绑定到'amountOfTeams'   因为它不是'p'的已知属性。 (“] [amountOfTeams] =”“[counter] =”“&gt;

”):ng:///e/e.html@1:14无法绑定到'counter',因为它不是'知道的   'p'的财产。 (“] [counter] =”“&gt;

1 个答案:

答案 0 :(得分:0)

您收到的错误是因为您尝试绑定到p元素的属性并不存在。

要将信息传递到结构指令中,请在指令的双引号内输入逻辑。

<p *while="some logic"></p>