我有以下自定义模型:
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);
});
}
我收到一个错误:
类型错误:无法读取未定义的属性“订阅”
我做错了什么?
模板是:
{{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}
在控制台中看到。但是进一步当我改变模型时没有任何变化。
可能您不需要订阅。您可以(ngModelChange)
根据自己的目的使用。更改模板输入:
<input [(ngModel)]="newEducationYearModel.name"
(ngModelChange)="modelChanged($event, newEducationYearModel)"
name="period"
type="text"
value=""/>
并向组件添加方法:
public modelChanged(year, newEducationYearModel) {
console.log('year', year);
console.log('newEducationYearModel', newEducationYearModel);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句