2017-12-19 18 views
0

私は学習中にAngularアプリケーションをまとめようとしています。モデルプロパティでデータバインドを使用するのに問題があります。角度のモデルプロパティでデータバインディングが機能しない

Iモデルを有する:のテンプレートに

import { Announcer} from '../models/announcer.model' 

    ....some code omited 

announcer: Announcer 
loading: Boolean 

constructor(private http: Http) { } 

ngOnInit() { 
     this.getRegister(); //I use this code to set the announcer variable 
} 


getRegister() : void 
{ 
    this.loading= true; 
    this.http.request('http://www.example.com/apiannouncer/show/0').subscribe((res: Response) => { 
    var result = res.json(); 



    this.announcer= new Announcer(result.name); 
    this.loading= false; 
}) 

}

export class Announcer{ 
    constructor(public name: string){} 
    } 

Iはregister.component.tsでモデルをインポートをannouncer.model.ts register.component.ts私はこれを使用できません:

<div> {{ announcer.name }} </div> 

私はこのメッセージが表示されます。

Uncaught (in promise): Error: Error class - inline template:2:5 caused by: self.context.announcer is undefined 
+0

controllerAs構文を使用していますか? – Rachmaninoff

+0

私は信じていません。 –

+1

'? '演算子を'

{{ announcer?.name }}
'のように使ってみましたか? – amal

答えて

0

あなたがherehereを見てみましょう、あなたの意見でcontrollerAs構文を使用する必要があります。

これにテンプレートを変更

<div ng-controller="MyController as vm" 
    <div> {{ announcer.name }} </div> 
</div> 

何それがないと、単に$scopeであなたのクラスのインスタンスを入れてvm経由$scopeにそのクラスを利用可能にしています。

関連する問題