我有一个观察者:
this.$scope.name = this.$state.current.name;
this.$scope.$watch(name, function () {
this.selectedTabIndex = this.$scope.getSelectedTabIndex();
});
当代码运行时:
Cannot read property '$scope' of undefined
如何使 this
在 $watch
中可用?
最佳答案
我相信您想使用箭头函数,因为函数
为this
创建了自己的作用域。
this.$scope.name = this.$state.current.name;
this.$scope.$watch(name, () => {
this.selectedTabIndex = this.$scope.getSelectedTabIndex();
});
关于angularjs - 这在 $watch 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54632975/