Accessing $scope in AngularJS factory?
You don't typically use $scope
inside a factory, service or provider. Usually, you would return the promise
(returned by $http
) and then handle the promise in a controller (where you do have $scope
).
factory.edit = function(id){
return $http.get('?controller=store&action=getDetail&id=' + id);
}
Controller function:
$scope.edit = function(id) {
deleteFac.edit(id).then(function(response) {
$scope.something = response.model;
});
}
I guess you meant this:
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;
});
Then in your controller you do:
function MyController($scope, deleteFac){
deleteFac.edit($scope.id, function(data){
//here you have access to your scope.
});
}
The following trick is a very bad practice, but you can use it if you are in a hurry:
Exchange the $scope
with: angular.element('[ng-controller=CtrlName]').scope()