以一本有角度的书为例,它们有以下代码:

<script>
   var test = {};


   var app = angular.module("app",[]);

   app.run(function($http){
      $http.get("/file").success(function(data){
         test.input = data;
      });
   });

   app.controller("ctrl",function($scope){
      $scope.handle = test;
   });


</script>

假设数据是从文件中提取的,推送到对象“test”,然后放入作用域的handle变量中。但是,当我写这篇文章时,问题是$http.get:
   app.run(function($http){
      $http.get("/file").success(function(data){
         test = data;
      });
   });

正在产生以下错误:
syntaxerror:意外字符串
at object.parse(本机)
在VC(http://localhost/common/js/frameworks/angular.min.js:15:480
在ZB(http://localhost/common/js/frameworks/angular.min.js:82:229
http://localhost/common/js/frameworks/angular.min.js:83:143
在m(http://localhost/common/js/frameworks/angular.min.js:7:322
在DD(http://localhost/common/js/frameworks/angular.min.js:83:125
在D(http://localhost/common/js/frameworks/angular.min.js:84:380
http://localhost/common/js/frameworks/angular.min.js:119:113
在N.A.$GET.N.$eval(http://localhost/common/js/frameworks/angular.min.js:133:221
在N.A.$get.N.$digest(http://localhost/common/js/frameworks/angular.min.js:130:233
(匿名函数)b.$get(匿名函数)
A.$get.n.$eval a.$get.n.$digest a.$get.n.$apply h k
Z.加载
对我来说,这表示$http不需要字符串url。有人能给我解释一下吗?这本书的样本代码是错的,还是过时了?
编辑:
文件内容如下:
[{“a”:“1”,“b”:“2”},{“aa”:“3”,“bb”:“4”}]
编辑2:
从其他so文章来看,$http.get似乎是自动解析的

最佳答案

你应该对数据进行编码。错误就会消失:

'[{"a":"1","b":"2"},{"aa":"3","bb":"4"}]'

09-10 06:17
查看更多