angularjs - 这在 $watch 中未定义

标签 angularjs

我有一个观察者:

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/

相关文章:

javascript - 未知提供者错误、依赖注入(inject)、模态 Controller

javascript - Node , Angular 错误 : ENOENT: no such file or directory

javascript - AngularJS 按类别过滤

javascript - 使用 ng-if 时 ng-click 停止工作

javascript - AngularJS 中的 ng-if 位于 ng-repeat 内

javascript - 使用 AngularJs 在 firebase 中创建用户

angularjs - 私有(private) angular.service 或 Controller

javascript - Angular UI Grid 重新加载单元格模板

javascript - 渲染 JavaScript 对象中的 HTML 标签

javascript - $state.go 不适用于 ng-click