2016-11-18 9 views
0

コンポーネントテンプレートからプレーンストリングを別のコンポーネントに渡すことはできますか?例えば...angular2の別のコンポーネントtplからコンポーネントに文字列を渡す

コンポーネント1つのTPL:

<survey-step-complete-msg [messageType]="'completed-survey'"></survey-step-complete-msg> 

コンポーネント2 .TS

import {Component, Input} from '@angular/core'; 
@Component({ 
    selector: 'survey-step-complete-msg', 
    templateUrl: '../../../../public/template/topic/survey-step-complete-msg.html' 
}) 
export class SurveyStepCompleteMsgComponent { 
    @Input() messageType; 
    constructor(){ 
     console.log(this.messageType); 
    } 
} 

コンポーネント2 TPL:

<div [ngSwitch]="messageType"> 

    <p *ngSwitchCase="completed-survey">Thanks</p> 
    <p *ngSwitchCase="survey-step-required-fields-missing">Stuff missing</p> 
    <p *ngSwitchCase="survey-required-fields-missing">Stuff missing</p> 
    <p *ngSwitchCase="survey-thanks-now-save">Thanks, don't forget to save.</p> 

</div> 

現在の結果は、TS要素であります@入力は常に定義されていません。

答えて

0

コンストラクタではまだ定義されていないため、未定義です。このconsole.logをngOnInit()メソッドに入れてください。それはそこに見えるはずです。

もマークアップは次のとおりです。

*ngSwitchCase="expression" 

完成-調査では表現できません。試してみてください:

<p *ngSwitchCase="'completed-survey'">Thanks</p> 
1

スイッチケースのステートメントも引用符で囲みます。

<p *ngSwitchCase="'completed-survey'">Thanks</p> 
0

早くこの問題が発生しました。あなたはする必要があります:[messageType] = "'completed-survey'の代わりにmessageType =" completed-survey ""

+0

ああ、よろしく!バンドルありがとう!そう多くの新しいsytax ..まだそれをすべて詰め込もうとしている:D – John

+0

実際には...それは解決されてうれしい – John

+0

が欠けていたスイッチの文字列の二重包囲だけだった。 –

関連する問題