在AngularJS工厂访问$作用域?

我是AngularJS的新手,发现它很有趣,但是我对以下情况有点不清楚。

app.factory('deleteFac', function($http){ var factory = {}; factory.edit = function(id){ $http.get('?controller=store&action=getDetail&id=' + id). success(function(data, status){ /** got an error on the following when i use return data; and i get data undefined in the controller which i get it because its doing a ajax call you don't get data until the call first. **/ $scope.detail = data; }) } return factory; }) 

我得到错误,当我分配给$scope和使用返回数据,有无论如何,我可以分配返回数据到$scope

你通常不使用工厂,服务或提供商内的$scope 。 通常情况下,你会返回promise (由$http返回),然后在一个控制器(你有$scope )的地方处理promise。

 factory.edit = function(id){ return $http.get('?controller=store&action=getDetail&id=' + id); } 

控制器function:

 $scope.edit = function(id) { deleteFac.edit(id).then(function(response) { $scope.something = response.model; }); } 

我想你的意思是:

 app.factory('deleteFac', function($http){ var service = {}; factory.edit = function(id, success, error){ var promise = $http.get('?controller=store&action=getDetail&id=' + id); if(success) promise.success(success); if(error) promise.error(error); }; return service; }); 

然后在你的控制器中做:

 function MyController($scope, deleteFac){ deleteFac.edit($scope.id, function(data){ //here you have access to your scope. }); } 

下面的技巧是一个非常糟糕的做法,但是如果您很匆忙,您可以使用它:

$scope与: angular.element('[ng-controller=CtrlName]').scope()

我认为这是最干净的解决scheme:

让我知道如果有问题或改善。

 (function(){ angular.controller('controllerName', controllerName); controllerName.$inject = ['$scope', factory]; function controllerName($scope, factory){ var vm = this; vm.data = factory.alertPopup(); } angular.factory('factory', factory); factory.$inject = ['externalServices']; function factory(externalServices){ return { returnData : returnData } function returnData(){ return externalServices.whatever(); } } })(); 

我个人希望从工厂使用范围,所以,而不是全部移出,我将范围作为参数从客户端调用factory.function()。

另外,当我尝试使用$ scope.watch(…)的时候,也遇到了同样的问题,因为我们不能直接在工厂或者服务中使用$ scope,但是我希望这样工作,所以我才更新了我的函数以scope作为参数,让工厂的客户端发送$ scope。 所以,这将是我的解决scheme:

 var app = angular.module("myApp", []); app.factory('MyFactory', function($http) { var factory = {}; //This is only for my own issue I faced. factory.Images = {}; factory.myFunction = function(id, scope) { //This is an example of how we would use scope inside a factory definition scope.details = "Initial Value"; //In my case I was having this issue while using watch scope.$watch('details' , function(newValue, oldValue) { if(oldValue){ scope.log = "Details was updated to : " +newValue; } }); scope.details = "My Id is: "+id; }; return factory; }); //Controller: Factory's Client. app.controller("MyController", ['$scope', 'MyFactory', function($scope, MyFactory) { MyFactory.myFunction(5, $scope); }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyController"> <span>{{details}} </span> <hr> <p>{{log}} </p> </div> 
 .factory('POPUP', function($ionicLoading, $ionicPopup) { var self = this; // THIS BLOCK SCREEN ! for loading ! Be carefoull !! ( deprecated: assign this to a var for security) self.showLoading = function(title, message, scope){ scope.loading = true; return $ionicLoading.show({ content: message, showBackdrop: false }); }; self.hideLoading = function(title, message, scope){ scope.loading = false; return $ionicLoading.hide(); }; // NOT BLOCK SCREEN- SIMPLE ALERTS - Standards popups self.showAlert = function(title, message, callback){ var alertPopup = $ionicPopup.alert({ title: title, template: message }); alertPopup.then(function(res) { console.log('callback popup'); if (callback){ callback(); } }); }; self.showConfirm = function(objectPopup, callback){ if (objectPopup === undefined){ objectPopup = { title: 'test confirm Popup', template: 'Message test Confirm POPUP' }; } var alertPopup = $ionicPopup.confirm(objectPopup); alertPopup.then(function(res) { if (res) { callback(true); } else { callback(false); } }); }; return self; })