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应相应更新自己的值,可是并没有更新;
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应相应更新自己的值,可是并没有更新;
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
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号