我有以下打字稿:
module MainCtrl {
interface IMainController {
getCustomers(): any;
}
class MainController implements IMainController {
static $inject = ['$scope', 'ApiServices','RememberSrvc'];
constructor(private $scope: any,
private ApiServices: ApiServices.IApiService,
private RememberSrvc: RememberSrvc.IRememberService) {
var vm = this;
}
getCustomers(){
this.ApiServices.get_request_params(, "")
.then(function(data) {
this.RememberSrvc.remember(data);//this is not working
}, function(err) {
});
}
}
angular.module('app').controller('MainCtrl', MainController);
}
我无法RememberSrvc
从then
街区访问。而且尽管我可以console.log响应。我无法将数据绑定到我的视图。
您可以使用箭头功能保持词法范围:
getCustomers() {
this.ApiServices.get_request_params(, "")
.then(data => this.RememberSrvc.remember(data), function (err) {
// handle error
});
}
或者,您也可以显式绑定上下文:
this.ApiServices.get_request_params(, "")
.then(this.RememberSrvc.remember.bind(this), function (err) {
// handle error
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句