javascript - angular2 - http模块作用域问题
巴扎黑
巴扎黑 2017-04-11 11:51:33
[JavaScript讨论组]

最近在学习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请求后的处理函数中获取构造器定义的变量?

巴扎黑
巴扎黑

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号