繁体   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