Angularjs 看奇怪的行为
我有以下$watch:Angularjs 看奇怪的行为,angularjs,typescript,Angularjs,Typescript,我有以下$watch: $scope.$watch('someParentClass.someClass.index', (newIndexValue: number) => { if (_.isNumber(newIndexValue)) { //Do something } }, true); 我想在以下几行中触发: 第1、2和3行: public foo(
$scope.$watch('someParentClass.someClass.index', (newIndexValue: number) => {
if (_.isNumber(newIndexValue)) {
//Do something
}
}, true);
我想在以下几行中触发:
第1、2和3行:
public foo(): someClass{
let obj: someClass;
obj = {
id?: number,
index: number,
};
obj.activeSectionIndex = 0; // Line 1
obj.activeSectionIndex = 1; // Line 2
obj.activeSectionIndex = 2; // Line 3
//...
return obj;
}
第4行:
public update(exploration: someClass): void {
//...
let index = 5;
exploration.index = index; // Line 4
//...
}
问题是手表只有在返回obj后才会触发在第一种情况下,代码>的值为obj.activeSectionIndex=2
(忽略值0和1的更新),在第4行的第二种情况下,根本不会触发
之前对类似问题的回答提出了以下解决方案:
- 不是复制值,而是创建引用。(通过创建一个新对象并更新exploration以引用它来尝试此操作。)
- 尝试更改回调以获取newVal和oldVal,而不仅仅是newVal
- 已尝试将objectEquality标志设置为true。并试图将其完全移除
- 已尝试将$watch更改为$scope。$watchCollection
这些解决方案都不起作用。
对如何解决这个问题有什么建议吗?谢谢。当你问一个由你的代码引起的问题时,如果你提供人们可以用来重现问题的代码,你会得到更好的答案。请参阅。当询问由您的代码引起的问题时,如果您提供人们可以用来重现问题的代码,您将获得更好的答案。看见