【发布时间】:2016-03-10 08:22:28
【问题描述】:
我正在尝试将 1.4 AngularJS 指令重构为 1.5 组件。我通过删除 $scope 并将其替换为 this 来尝试此操作。
到目前为止它工作正常,除了:我需要在回调函数中设置一个$scope 变量。像这样:
this.variable = {};
someFunction().then(function(newValue) {
this.variable = newValue;
});
但是,
this在回调函数中是未定义的。
设置this.variable 值的解决方法或正确方法是什么样的?
【问题讨论】:
-
这可能有助于理解它发生的原因。 toddmotto.com/understanding-the-this-keyword-in-javascript 它是 JavaScript 的核心概念。下面的答案向您展示了解决方案。
标签: javascript angularjs