Home > Backend Development > PHP Tutorial > 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?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 09:21:07
Original
851 people have browsed it

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
angular.js - angularJS ng-style用法
From 1970-01-01 08:00:00
0
0
0
angular.js - AngularJS form validation
From 1970-01-01 08:00:00
0
0
0
angular.js - Learning AngularJS
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template