我有以下自定義模式:如何訂閱自定義模型Angular 2?
export class CreateEducationYear {
public year: number;
public name: string;
}
我用這部分是這樣的:
public newEducationYearModel: Observable<CreateEducationYear>;
constructor() {
this.newEducationYearModel = new Observable<CreateEducationYear>();
}
// Subscribe method
public test(): Observable<CreateEducationYear> {
return this.newEducationYearModel;
}
// Listening
ngOnInit() {
this.test().subscribe(data => {
console.log(data);
});
}
我得到一個錯誤:
TypeError: Cannot read property 'subscribe' of undefined
我該怎麼辦錯了嗎?
模板是:
{{newEducationYearModel | json }}
<div class="filter-search-item-sub col-md-3">
<label>Название периода</label>
<input [(ngModel)]="newEducationYearModel.name" name="period" type="text" value=""/>
</div>
後首次發射我控制檯中看到CreateEducationYear {year: 2000}
。但進一步,當我改變模型沒有什麼變化。
你是否試圖用自定義對象來嘲笑結果? – Arun
你想做什麼?返回可觀察()? –
我嘗試當它在模板 – user3573738