我正在使用AngularJS 1.3.5,我試圖從json文件中獲取信息。 這裏是我的代碼: HTML文件:
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<script src="js/controllers/MainController.js"></script>
<script src="js/services/myapp.js"></script>
<script src="js/app.js"></script>
<body ng-app="app">
<div ng-controller="MainController">
<div ng-repeat="content in contents">
\t \t \t
<a ng-href="{{data.FolderPath}}">{{data.FolderPath}}</a>
\t \t \t \t \t
</div>
</div>
</body>
</html>
Javascript文件: MainController.js
app.controller('MainController', ['$scope', 'myapp', function($scope, myapp) {
myapp.success(function(data) {
$scope.FolderPath = data;
});
}]);
myapp.js
app.factory('myapp', ['$http', function($http) {
return $http.get('C:\Users\nouri\Desktop\configFile.json')
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}]);
最後app.js
var app = angular.module('FolderApp', []);
你不能從異步函數返回。請參閱[如何從異步調用返回響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Tushar