2016-11-09 6 views
0

結合:Angular2:入力、ここで定義されるように、私はコンポーネントで入力をバインドしようとしています

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child しかしコンポーネント名が

私もにconsole.logをやろうとしていますが示されることはありませんが、それは示しています

コンポーネント名が

をisundefined

error.htmlで

<div> you are not allowed to access {{component}} </div> 

デモエラー:

import { Component, OnInit,Input } from '@angular/core'; 

@Component({ 
    selector: 'demo-error', 
    templateUrl: './error.component.html', 
    styleUrls: ['./error.component.css'] 
}) 
export class ErrorComponentDemo implements OnInit { 
@Input() public componentName:string; 
    constructor() { 
    console.log("componentName is" +this.componentName) 
    } 

    ngOnInit() { 
    } 

}

And in **CustomerComponent:** 

@Component({ 
    selector: 'customer', 
    templateUrl: './customer.component.html', 
    styleUrls: ['./customer.component.css'] 
}) 
export class CustomerComponent extends SuperChildComponent{ 
    public allowed: boolean = false; 
    public accessLevel:AccessLevel =null; 
    public componentname:string; 

    constructor(private authenticationService : AuthorizationService) { 
    super(); 
    this.componentname=this.constructor.name; 
    this.accessLevel=this.authenticationService.isUserLoggedIn()?this.authenticationService.componentAccessLevel(this.constructor.name):null; 
    } 

私はここで何をしないのですか?

ありがとうございました

答えて

2

あなたの入力はまだコンストラクタで利用できません。これを試してください。

@Input set componentName(value: string) { 
    if(value != null && value != undefined) { 
    this._componentName = value; console.log(this._componentName); 
    } 
} 

_componentName: string 

あなたがあなたの入力した文字列の場合は「[]」を失うする必要がset

2

if文でメソッドのあなたの通話を行うことができますこの方法。

componentName="componentname" 
関連する問題