0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @zps215 , it did the trick! I had been stuck on this for about 3 hours now.
yeah it happens sometimes. just a reminder. don't forget to mark correct answer and like it :) cause sharing is caring

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.