[英]Accessing $scope in AngularJS factory?
我是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
返回),然后在控制器中處理promise(您有$scope
)。
factory.edit = function(id){
return $http.get('?controller=store&action=getDetail&id=' + id);
}
控制器功能:
$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.
});
}
以下技巧是一種非常糟糕的做法,但如果你趕時間,你可以使用它:
使用: angular.element('[ng-controller=CtrlName]').scope()
交換$scope
angular.element('[ng-controller=CtrlName]').scope()
我個人想從工廠使用作用域,所以,我不會全部移動,而是將作為參數的作用域傳遞給調用factory.function()的客戶端。
當我嘗試使用$ scope.watch(...)時,我也遇到了同樣的問題,因為我們不能直接使用工廠或服務的$ scope,但我希望這樣工作,所以這就是為什么我剛剛更新了我的函數有作為參數的范圍,讓工廠的客戶端發送$ scope。 所以,這將是我的解決方案:
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>
我希望這可以提供幫助。 問候。
我認為這是最干凈的解決方案:
如果有問題或改進,請告訴我。
(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('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;
})
我知道這個問題很老,但這對我有用
app.factory('myFactory',function(){
let toRet = {
foo: foo
}
return toRet;
function foo(){ // This function needs to use passed scope.
let $scope = toRet.$scope;
// Do stuff with $scope.
}
});
app.controller('myController',function($scope,myFactory){
myFactory.$scope = $scope;
/*
We could just pass $scope as a parameter to foo, but this is
for cases where for whatever reason, you cannot do this.
*/
myFactory.foo();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.