Search code examples
angularjsangular-factory

unpr error in angularjs while using factory from another file


Hello I have two angularjs files 1st one is controller and second is factory function but it gives an unpr error

factory file is

var app = angular.module('basicApi',[]);
app.factory('webApi',function($http,$q,$scope){
    return {
        login : function(data){
            return $http.post('/user/login',data)
            .then(function(res) {
                if(res.status === '403'){
                   return $q.reject("Invalid credentials");
                }
            })
            .catch(function(err){
                return $q.defer('Cannot make API Call');
            })
        }
    }
});

controller file is

angular.module('App',["basicApi"])

.controller('registerController',["$scope","webApi",
function($http,$scope,$window,webApi){
//login function 
$scope.login = function() {
    var data = {
        email: $scope.email,
        password: $scope.password
    }
    webApi.login(data)
    .then(function(res){
        // flash message or err message in front end
    },function(err){
        //err message 
    })
}

}])

i already added both of files to my html and both are working angular error in console is

angular.js:14525 Error: [$injector:unpr] http://errors.angularjs.org/1.6.4/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20webApi

Solution

  • You can't inject $scope inside factory/proider/service(in general in none of provider flavours).

    $scope can be available as injectable in controller function. As controller logic binds to particular template with its scope.

    Remove $scope would fix your issue.

    app.factory('webApi',function($http,$q){