php - Angularjs中的factory在promise之后,如何更新在controller中的数据?
怪我咯
怪我咯 2017-04-11 09:13:27
[PHP讨论组]
app.factory('mainClass',function($http,mainFac){
        var mainClass=function(){
            this.uid;
            this.sid;
            this.getUid();
        }
        mainClass.prototype.getUid=function(){
            var promise = mainFac.query('OPT','PARM1','PARM2');
            promise.then(function(data){
                console.info("mainClass is :",data);
                this.sid=data.sid;
                console.info("this.sid :",this.sid);
            });
        };
        return mainClass;
    });
    
app.controller('perCenterCtrl', function($scope, $http, $state, ngDialog,
            qfact, myfactory,mainFac,mainClass) {
        var mainObj=new mainClass();
        console.info("mainObj is :",mainObj);
        $scope.sid=mainObj.sid;
});

代码目的:
controller顺序执行,遇到名为mainClass的factory初始化,mainClass异步初始化,从后台拿到数据并更新自己的this.sid,此时在controller中也更新$scope.sid;
遇到困难:
我的理解是:$scope.sid=mainObj.sid;已经绑定了,在mainClass执行过程,异步地从后台拿到数据并更新自己的this.sid后,$scope.sid应相应更新自己的值,可是并没有更新;

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
PHP中文网
app.factory('mainClass',function(mainFac){
        function getUid(){
            mainFac.query('OPT','PARM1','PARM2').then(function(response){
                return response;
            },function(error){
                return error;
            });
        }
        
        return {getUid};
    });
    
app.controller('perCenterCtrl', function($scope, $http, $state, ngDialog,
            qfact, myfactory,mainFac,mainClass) {
        mainClass.getUid().then(function(data){
            console.info("mainObj is :",data);
            $scope.sid=data.sid;
        });
});

没太看懂,你这么写写试试。建议你看看这个:http://each.sinaapp.com/angular/tutorial/ng-factory.html

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

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