2017-05-29 14 views
0

角度私Angular2アプリでTabTitleComponentと呼ばれるコンポーネントがあります。@Input()が動作していない - 2

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

     @Component({ 
     selector: 'tab-title', 
     templateUrl: './tab-title.component.html', 
     styleUrls: ['./tab-title.component.css'] 
     }) 
     export class TabTitleComponent implements OnInit,AfterViewInit { 

     @Input() title; 

     ngOnInit() 
     { 
      console.log('ngOnInit'+this.title); 
     } 
     ngAfterViewInit() 
     { 
      console.log('ngAfterViewInit',this.title); 
     } 

     } 

私はAppComponentにこのコンポーネントを使用しています。

私はHTMLでTabTitleComponentAppComponentのテンプレート)を使用して問題1は、次のように私はtitle値を取得しておりません。コンソールでは、undefinedをログに記録されます。

<div> 
    <tab-title [title]='88'> </tab-title>  
</div> 

私はそれを次のように置き換えると、コンソールに88が表示されます。

<tab-title [title]='88'> </tab-title> 

私はstringタイプのtitleを作り、私はundefinedを取得していますどちらの場合も、値を渡す問題2。

私は、根本的な原因を見つけることができないのです。誰かが問題を見つけるのを助けることができますか?

答えて

1

"88" でたぶん理由 このコードを試してみてください。

<tab-title [title]="'88'"> </tab-title> 
+0

'使用した後は、 " '名前'"'それが働いているが。それは変だね 。なぜそうなのか? @Max – Dalvik

+2

「名前は」 - 単なる文字列 - TSファイル内の変数と、「『名前』」です。 –

+0

はそれを得ました。私が使用しclearification – Dalvik

1
//our root app component 
import {Component, Input, NgModule, VERSION} from '@angular/core' 
import {BrowserModule} from '@angular/platform-browser' 


@Component({ 
    selector: 'tab-title', 
    template: '<div>{{title}}</div>' 
}) 
export class TabTitleComponent implements OnInit,AfterViewInit { 

    @Input() title : string; 

    ngOnInit() 
    { 
    console.log('ngOnInit'+this.title); 
    } 
    ngAfterViewInit() 
    { 
    console.log('ngAfterViewInit',this.title); 
    } 

} 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
    <tab-title [title]="'88'"> </tab-title>  
    </div> 
    `, 
}) 
export class App { 

} 

@NgModule({ 
    imports: [ BrowserModule ], 
    declarations: [ App, TabTitleComponent ], 
    bootstrap: [ App ] 
}) 
export class AppModule {} 
+0

ためthnks '「『名前』」それが働いています。その仕事の背後にある理由は何ですか? – Dalvik

+0

入力フォーマットは、[InputName] = "パラメータ" であります –

関連する問題