简体   繁体   English

将数据从组件传递到指令

[英]Passing data to directive from component

I have following directive code: 我有以下指令代码:

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....

    }
}

My component 我的组件

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);
    }

}

I have loaded WhileDirective in declarations in my app.module.ts 我已经在我的app.module.ts中的声明中加载了app.module.ts

Unfortunately I'm getting an error while compiling: 不幸的是,编译时出现错误:

Uncaught Error: Template parse errors: Can't bind to 'amountOfTeams' since it isn't a known property of 'p'. 未捕获的错误:模板解析错误:由于它不是'p'的已知属性,因此无法绑定到'amountOfTeams'。 (" ][amountOfTeams]="" [counter]=""> (“] [amountOfTeams] =”“ [counter] =”“>

"): ng:///e/e.html@1:14 Can't bind to 'counter' since it isn't a known property of 'p'. (" ][counter]=""> “):ng:///e/e.html@1:14无法绑定到'counter',因为它不是'p'的已知属性。(”] [counter] =“”>

The error that you're receiving is because you're trying to bind to a property of the p element which doesn't exist. 您收到的错误是因为您试图绑定到不存在的p元素的属性。

To pass information into your structural directive, enter your logic within the double quotes of your directive. 要将信息传递到结构指令中,请在指令的双引号中输入逻辑。

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

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

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