如何使用angularjs从json获取数据

Roh*_*nay 0 angularjs

我有这样的样本数据

var contacts = [{
    id: 0,
    'name': 'john',
        'email': 'hello@gmail.com',
        'phone': '123-2343-44'
}];
Run Code Online (Sandbox Code Playgroud)

而是使用这种方式,我如何使用angularjs从json文件中获取数据?

mgn*_*gnb 6

查看$ http服务.

简而言之,例如,如果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 
  });
});
Run Code Online (Sandbox Code Playgroud)