最近在学习angular2,写demo时用到其中的http模块,代码如下
app.loginComponent =
ng.core.Component({
selector: 'login',
templateUrl: 'app/login/login.html',
providers:[lazyResolveService(['app.loginService'])]
}).Class({
constructor: [
ng.http.Http,
lazyResolveService(['app.loginService']),
function (http,service) {
this.result = 'hello world';
this.httpService = http;
this.loginService = service;
// this.loginService = app.loginServiceInjector.get(app.loginService);
}
],
login: function (username) {
//this.loginService.printName(username);
this.httpService.get('http://localhost:8080/xxx/login')
.map(function (res) {
var str2 = JSON.parse(res._body);
return str2.message.securityMessage;
})
.subscribe(function (res) {
console.log(res);
return this.result = 'login';
});
console.log(this.result);
}
});
在http.get处理到subscribe()这里代码:
console.log(res);
return this.result = 'login';
时,发现里面的this.result不是构造器里定义的this.result,而是subscribe()里自己重新定义的this.result,导致最后的console.log(this.result)一直输出‘hello world’.
请问怎么在http请求后的处理函数中获取构造器定义的变量?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号