Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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