请问一下如何用angularjs读取本地json?

如何用angularjs读取本地json


慕虎7371278
浏览 1264回答 3
3回答

慕村9548890

很明显你是理解错了执行的先后顺序,你这样测试下:$scope.callback=function(){console.log($scope.phones)//输出undefinedconsole.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请求如果你没设定同步的话,请求后面定义的代码会先执行});

慕工程0101907

用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;});});
打开App,查看更多内容
随时随地看视频慕课网APP