我有一个带有这样的嵌套对象的观察对象
signupVM = new observableModule.fromObject({
editIcon: "\ue3c9",
editingMode: false,
signupVal: {
name: settings.getString("userName"),
phone: "",
altPhone: "",
businessName: "",
businessAddress: ""
},
});
我稍后会在代码中尝试设置phone
的值,但不会更新可观察值。这是我尝试过的:
signupVM.set("signupVal.phone", response.data.phone);
signupVM.set("phone", response.data.phone);
它们都不起作用。我该怎么办?
答案 0 :(得分:0)
每个字段都必须是可观察的,以使更改反映在View层中。
您将需要调用NativeScripts通知功能。
我使用Typescript,因此在纯JS中的操作会略有不同:
this.notify({
eventName: Observable.propertyChangeEvent,
propertyName: name,
object: this, // the property that will be changed
value: val // new value to be displayed, may be different then the actual prop value
});
装饰解决方案(TS):
// property of a TS class
@ObservableProperty() style: string;
function ObservableProperty() {
return (target: Observable, propertyKey: string) => {
Object.defineProperty(target, propertyKey, {
get() {
return this["_" + propertyKey];
},
set(value) {
if (this["_" + propertyKey] === value) {
return;
}
this["_" + propertyKey] = value;
this.notifyPropertyChange(propertyKey, value);
},
enumerable: true,
configurable: true
});
};
}