Ayush Gupta
Ayush Gupta

Reputation: 1727

Angular unknower provider error in custom service

This is my code for initializing the app and creating a controller.

var app = angular.module('newstalk',[]);

app.controller("articleCtrl",['$scope','$http','dataService',function($scope,$http,dataService){

    $scope.articles = dataService.getArticles();
    $scope.commentForm = function(id,userid){
        console.log(userid);
        var uid = userid;
        var c =  this.contents;
        var data = {
            content: c,
            user: uid
        };
        console.log(data);
        $http.post('/api/article/'+id,data);
    };
}]);

Now, I have also created a service to fetch the data from the server. Here is the code for that:

(function(){

    angular.module('newstalk')
        .factory('dataService',dataService);

    function dataService(){
        return {
            getArticles : getArticles
        };

        function getAricles(){
            console.log("yolo");
            return $http({
                method:get,
                url:'/api/articles/0'
            })
            .then(sendResponse);
        }

        function sendResponse(response){
            console.log(data);
            return response.data;
        }
    }

})

This is in a seperate file. Now when I run this I get a Error: $injector:unpr Unknown Provider error. I've read multiple other such questions, none of which came to help. Any ideas?

Upvotes: 0

Views: 32

Answers (1)

Zeel Shah
Zeel Shah

Reputation: 462

I think you have not used IIFE correctly.

you should put () at the end of file.

(function(){

angular.module('newstalk')
    .factory('dataService',dataService);

function dataService(){
    return {
        getArticles : getArticles
    };

    function getAricles(){
        console.log("yolo");
        return $http({
            method:get,
            url:'/api/articles/0'
        })
        .then(sendResponse);
    }

    function sendResponse(response){
        console.log(data);
        return response.data;
    }
}

})()

putting () execute/run the function. rightnow you are not executing IIFE.

Upvotes: 1

Related Questions