I have my sample data like this
var contacts = [{
id: 0,
'name': 'john',
'email': '[email protected]',
'phone': '123-2343-44'
}];
Instead using this way, how do i fetch the data from a json file using angularjs?
I have my sample data like this
var contacts = [{
id: 0,
'name': 'john',
'email': '[email protected]',
'phone': '123-2343-44'
}];
Instead using this way, how do i fetch the data from a json file using angularjs?
Check out the $http service.
In short, if for instance the json endpoint is at the same web domain at /contacts_endpoint.json, include $http in your controller/service/whatever and run
angular.module('myModule').controller(function($scope, $http) {
$http.get('/contacts_endpoint.json').success(function(contacts) {
// the variable contacts contains the data you want
});
});
var url = 'http://localhost/contacts'; contacts = {}; contacts.getConacts = function() { return $http.get(url + '/all'); }First of all, your JSON is not valid. Provided you have a valide one just write
var objectFromJSON = JSON.parse(validJSON);
and access any part of original JSON you want to.