Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AngularJS Basics (1.x) Services in Angular Using Services To Get Data

Gavin Murphy
Gavin Murphy
10,004 Points

Not pulling json data

ng-repeat was working correctly until I moved my array to a json file and wrote a service request as shown in the video. Please help.

2 Answers

Gavin Murphy
Gavin Murphy
10,004 Points
angular.module("todoListApp", [])
.controller("mainCtrl",function($scope, dataService){


    dataService.getTodos(function(response){
        console.log(response.data);
        $scope.todos = response.data;

        });

})
.service('dataService', function($http){

    this.getTodos = function(callback){
        $http.get('mock/todos.json')
        .then(callback)
        };
});
andi mitre
andi mitre
Treehouse Guest Teacher

Is this a specific treehouse challenge? Did you put it in the above directory and what are some of the errors you see in the console if any?

Cheers, Andi

Mars Epaecap
Mars Epaecap
5,110 Points

Gavin Murphy i love you. Thanks for this. This treehouse course is letting me down

Gavin Murphy
Gavin Murphy
10,004 Points

Hi Andi, thanks for your reply, I messed up this question when I was asking it. The problem was that I couldn't reference the json file while it was static and not on a server, it had to be hosted for the .services directive to work... I appreciate all the help I have got through this. Its a good feeling when people are willing to help you :)

andi mitre
andi mitre
Treehouse Guest Teacher

+1 for solving your own question. We're always here to help! :)

Cheers