从angularjs中的本地JSON文件中获取数据

Ami*_*mit 21 json angularjs

我想从我本地计算机上的JSON文件中获取数据.但我无法获得数据.它显示$ http的一些跨域错误.

这是我的代码.

angular.module('myApp',[])
  .controller('myCtrl', function ($scope, webtest) {
     webtest.fetch().then(function (data) {
         $scope.accounttype = data;
     })
});

.factory('webtest', function($q, $timeout, $http) {
    var Webtest = {
        fetch: function(callback) {
            return $timeout(function() {
                return $http.get('webtest.json')
                .then(function(response) {
                      return response.data;
                });
            }, 30);
         }
    };
    return Webtest;
}); 
Run Code Online (Sandbox Code Playgroud)

有人请帮我如何显示本地JSON文件中的数据?提前致谢.

小智 37

这很简单

$http.get('phones/phones.json').then(function(response) {
   $scope.phones = response.data;
});
Run Code Online (Sandbox Code Playgroud)

请参阅:http://stackoverflow.com/questions/21589340/read-local-file-in-angularjs

  • .success已弃用.使用.then代替;) (4认同)