LearnToday
LearnToday

Reputation: 2902

Get data through a service

I created a service called servcises/employees.js:

angular.module('dashyAppApp')
  .service('employees', function () {
    this.getEmployees = function() {
      return $.get( '/data/employee.json' );
    };
  });

This service is suppose to read the json file in the data folder.

{
  "countries": [
    {
        "country": "Cameroon",
        "employ_count": 50,
    },
    {
        "country": "United States",
        "employ_count": 738
    }
]
}

Here is my controllers/main.js :

.controller('MainCtrl', function ($scope, employees, Markers) {
    var _this = this;
    employees.getEmployees().then(
      function(data) {
    _this.items = data;
      }
    );
});

And here is my view:

<div  ng-repeat="item in main.items.countries">
      <h4>{{item.country}}</h4>
    </div>

unfortunately nothing is being displayed. I am not sure of what am doing wrong.

Upvotes: 1

Views: 58

Answers (1)

Arun Ghosh
Arun Ghosh

Reputation: 7764

User $http to get data via AngularJS:

angular.module('dashyAppApp')
  .service('employees', function ($http) {
    this.getEmployees = function() {
      return $http.get( '/data/employee.json' );
    };
  });

And additional it seems that you JOSN has an extra comma after "employ_count": 50. Can you try removing it?

Upvotes: 4

Related Questions