Home  >  Article  >  Backend Development  >  How does the factory in Angularjs update the data in the controller after the promise?

How does the factory in Angularjs update the data in the controller after the promise?

WBOY
WBOYOriginal
2016-08-04 09:21:07789browse

<code>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;
});
</code>

Purpose of the code:
The controller is executed sequentially, and when it encounters the factory initialization named mainClass, mainClass is initialized asynchronously, gets the data from the background and updates its own this.sid. At this time, $scope.sid is also updated in the controller;
When it encounters Difficulty:
My understanding is: $scope.sid=mainObj.sid; has been bound. During the mainClass execution process, after asynchronously getting data from the background and updating its own this.sid, $scope.sid should be updated accordingly. My own value, but it is not updated;

Reply content:

<code>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;
});
</code>

Purpose of the code:
The controller is executed sequentially, and when it encounters the factory initialization named mainClass, mainClass is initialized asynchronously, gets the data from the background and updates its own this.sid. At this time, $scope.sid is also updated in the controller;
When it encounters Difficulty:
My understanding is: $scope.sid=mainObj.sid; has been bound. During the mainClass execution process, after asynchronously getting data from the background and updating its own this.sid, $scope.sid should be updated accordingly. My own value, but it is not updated;

<code>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;
        });
});
</code>

I don’t quite understand it, try writing it like this. I suggest you take a look at this: http://each.sinaapp.com/angular/tutorial/ng-factory.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn