【发布时间】:2016-09-28 09:25:14
【问题描述】:
为什么我在构造函数之外没有$scope 的范围,除非我使用粗箭头定义我的函数?或者是否可以在不使用粗箭头定义函数的情况下访问 $scope?
namespace FooBar {
export interface MyScope extends ng.IScope {
message: string;
}
export class SandboxCtrl {
static $inject = ["$scope", "$timeout"];
private scope: MyScope;
private timeout: ITimeoutService;
constructor($scope: MyScope, $timeout: ng.ITimeoutService) {
this.scope = $scope;
this.timeout = $timeout;
timeout(this.foo, 1000); // does not work
timeout(this.bar, 1000); // works
}
public foo() {
this.scope.message = "foo bar"; // does not work
}
bar = () => {
this.scope.message = "foo bar"; // works
}
}
}
更新我注意到我没有分享整个问题,因为我不知道这是因为$timeout 指令导致了问题。无论如何,我更新了我的示例。
【问题讨论】:
-
你有没有在你的项目中添加了 DefinedTyped angualer 文件?
-
@NinjaDeveloper 是的,我有。
标签: angularjs typescript angularjs-scope