I have my sample data like this
我有这样的样本数据
var contacts = [{
id: 0,
'name': 'john',
'email': 'hello@gmail.com',
'phone': '123-2343-44'
}];
Instead using this way, how do i fetch the data from a json file using angularjs?
而是使用这种方式,我如何使用angularjs从json文件中获取数据?
2 个解决方案
#1
6
Check out the $http service.
查看$ http服务。
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
简而言之,例如,如果json端点位于/contacts_endpoint.json的同一个Web域,请在控制器/ service / whatever中包含$ http并运行
angular.module('myModule').controller(function($scope, $http) {
$http.get('/contacts_endpoint.json').success(function(contacts) {
// the variable contacts contains the data you want
});
});
#2
0
First of all, your JSON is not valid. Provided you have a valide one just write
首先,您的JSON无效。如果你有一个valide,只需写
var objectFromJSON = JSON.parse(validJSON);
and access any part of original JSON you want to.
并访问您想要的原始JSON的任何部分。
#1
6
Check out the $http service.
查看$ http服务。
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
简而言之,例如,如果json端点位于/contacts_endpoint.json的同一个Web域,请在控制器/ service / whatever中包含$ http并运行
angular.module('myModule').controller(function($scope, $http) {
$http.get('/contacts_endpoint.json').success(function(contacts) {
// the variable contacts contains the data you want
});
});
#2
0
First of all, your JSON is not valid. Provided you have a valide one just write
首先,您的JSON无效。如果你有一个valide,只需写
var objectFromJSON = JSON.parse(validJSON);
and access any part of original JSON you want to.
并访问您想要的原始JSON的任何部分。