TypeScript/angularJS HTTP GET请求中的作用域
我不熟悉typescript和angular.js,我正在努力处理http get请求。 我使用的是angular的类型定义 我的控制器代码如下所示:TypeScript/angularJS HTTP GET请求中的作用域,angularjs,typescript,angularjs-scope,Angularjs,Typescript,Angularjs Scope,我不熟悉typescript和angular.js,我正在努力处理http get请求。 我使用的是angular的类型定义 我的控制器代码如下所示: module game.Controller { 'use strict'; export interface IGameScope extends ng.IScope { vm: GameCtrl; } export class GameCtrl { private bonu
module game.Controller {
'use strict';
export interface IGameScope extends ng.IScope {
vm: GameCtrl;
}
export class GameCtrl {
private bonus: any;
private http: any;
constructor($scope: IGameScope, $http: ng.IHttpService, $location: ng.ILocationService) {
$scope.vm = this;
this.http = $http;
}
doBet() {
this.http.get('http://localhost:9000/db').success(function(data: any, status: any) {
this.bonus = data;
}
);
}
}
}
<button ng-click="vm.doBet()">bet</button>
<div><span>bonus: {{ vm.bonus }}</span></div>
constructor($scope: IGameScope, $http: ng.IHttpService, $location: ng.ILocationService) {
$scope.vm = this;
this.http = $http;
$scope.doBet = function() {
this.http.get('http://localhost:9000/db').success(function (data: any, status: any) {
$scope.bonus = data;
});
}
}
我的观点是:
module game.Controller {
'use strict';
export interface IGameScope extends ng.IScope {
vm: GameCtrl;
}
export class GameCtrl {
private bonus: any;
private http: any;
constructor($scope: IGameScope, $http: ng.IHttpService, $location: ng.ILocationService) {
$scope.vm = this;
this.http = $http;
}
doBet() {
this.http.get('http://localhost:9000/db').success(function(data: any, status: any) {
this.bonus = data;
}
);
}
}
}
<button ng-click="vm.doBet()">bet</button>
<div><span>bonus: {{ vm.bonus }}</span></div>
constructor($scope: IGameScope, $http: ng.IHttpService, $location: ng.ILocationService) {
$scope.vm = this;
this.http = $http;
$scope.doBet = function() {
this.http.get('http://localhost:9000/db').success(function (data: any, status: any) {
$scope.bonus = data;
});
}
}
如何实现在success函数中更新变量
如果有更好/更干净的方法或实践来更新请求中的数据,我也将不胜感激实际上是指
success
中的回调函数
相反,您可以这样做:
$scope.vm.bonus=data代码>您可以将该方法放入构造函数中,以便访问$scope,如下所示:
module game.Controller {
'use strict';
export interface IGameScope extends ng.IScope {
vm: GameCtrl;
}
export class GameCtrl {
private bonus: any;
private http: any;
constructor($scope: IGameScope, $http: ng.IHttpService, $location: ng.ILocationService) {
$scope.vm = this;
this.http = $http;
}
doBet() {
this.http.get('http://localhost:9000/db').success(function(data: any, status: any) {
this.bonus = data;
}
);
}
}
}
<button ng-click="vm.doBet()">bet</button>
<div><span>bonus: {{ vm.bonus }}</span></div>
constructor($scope: IGameScope, $http: ng.IHttpService, $location: ng.ILocationService) {
$scope.vm = this;
this.http = $http;
$scope.doBet = function() {
this.http.get('http://localhost:9000/db').success(function (data: any, status: any) {
$scope.bonus = data;
});
}
}
这里有一个关于将AngularJS与Typescript一起使用的示例。我没有使用Typescript,但对我来说,它看起来像一个闭包/范围问题。您的成功回调是异步运行的,因此this
的值在内部是不同的。尝试使用this
绑定函数回调
this.http.get('http://localhost:9000/db').success(angular.bind(this,
function(data: any, status: any) {this.bonus = data;});
这可以使用TypeScript的lambda表达式轻松完成:
doBet() {
this.http.get('http://localhost:9000/db').success(
(data, status) => this.bonus = data
);
}
这不起作用,因为$scope
仅在构造函数中可见,而在doBet()
函数中不可见。@3x14159265将doBet()
移动到构造函数中,并将其声明为$scope
上的方法:$scope.doBet=function(){…}
,或者只需将$scope
传递到doBet()
@3x14159265,我最好将doBet
移动到控制器内,并在scope上声明。谢谢,我已经阅读了本教程。但在控制器中定义所有作用域函数真的是最佳实践吗?此外,我将$scope.vm
变量绑定到我的控制器实例,以省去将每个范围变量绑定到控制器变量的需要。因此,在这种情况下,使用绑定$scope.vm=this不会有任何增强代码>@3x14159265我认为使用TypeScript真的太过分了,它使JavaScript变得复杂。这在TypeScript中是不必要的。请参阅3x14159265的答案。您对此主题有更多的解释吗?例如,为什么我们必须在这里使用lambda表达式?在普通的js中,您将拥有例如this.http.get('url').success(函数(数据,状态){…})代码>,因此此
在成功函数中不可见。如上所示的lamba表达式自动编译为var\u this=this;this.http.get('url').success(函数(数据、状态){u this.member=“something”;})
,因此通过helper变量\u this
使此
在作用域中可见。希望有帮助。