在Angular2/Typescript中,是否可以"观察"对象的字段以进行更改.
例如,假设我有一个类Person与领域firstName,lastName和fullName.是否有可能自动更新fullName每当要么firstName或lastName改变?
像这样的东西:
export class Person {
firstName: string= '';
lastName: string = '';
fullName: string = '';
constructor(firstName: string, lastName: string) {
this.firstName.onChange(() => { this.updateFullName(); });
this.lastName.onChange(() => { this.updateFullName(); });
this.firstName = firstName;
this.lastName = lastName;
}
updateFullName() {
this.fullName = `${this.firstName} ${this.lastName}`;
}
}
Run Code Online (Sandbox Code Playgroud)