3 回答

TA貢獻1884條經驗 獲得超4個贊
很明顯你是理解錯了執(zhí)行的先后順序,你這樣測試下:
$scope.callback=function(){
console.log($scope.phones)//輸出undefined
console.log(test)//輸出空Object
}
var test=new Object();
$http.get('phones/phones.json').success(function(data)
{
$scope.phones = data;
test = data;
console.log($scope.phones)//正常輸出JSON對象
console.log(test)//正常輸出JSON對象
$scope.callback();//換句話就是,ajax請求如果你沒設定同步的話,請求后面定義的代碼會先執(zhí)行
});

TA貢獻1887條經驗 獲得超5個贊
用angularjs讀取本地json的方法如下:
1、本地json文件的內容如下:
[{ "text":"learn angular", "done":true },
{ "text":"build an angular app", "done":false},
{ "text":"something", "done":false },
{ "text":"another todo", "done":true }]
2、利用angularjs讀取的方法:
var App = angular.module('App', []);
App.controller('TodoCtrl', function($scope, $http) {
$http.get('todos.json')
.then(function(res){
$scope.todos = res.data;
});
});
添加回答
舉報