簡體   English   中英

將數據從組件傳遞到指令

[英]Passing data to directive from component

我有以下指令代碼:

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中的聲明中加載了app.module.ts

不幸的是,編譯時出現錯誤:

未捕獲的錯誤:模板解析錯誤:由於它不是'p'的已知屬性,因此無法綁定到'amountOfTeams'。 (“] [amountOfTeams] =”“ [counter] =”“>

“):ng:///e/e.html@1:14無法綁定到'counter',因為它不是'p'的已知屬性。(”] [counter] =“”>

您收到的錯誤是因為您試圖綁定到不存在的p元素的屬性。

要將信息傳遞到結構指令中,請在指令的雙引號中輸入邏輯。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM