Javascript AngularJS$http.get返回未定义的
我正在构建一个小的角度应用程序。我从这里开始:。我只更改了以下文件:Javascript AngularJS$http.get返回未定义的,javascript,json,angularjs,Javascript,Json,Angularjs,我正在构建一个小的角度应用程序。我从这里开始:。我只更改了以下文件: /app/view1/view1.js "严格使用", angular.module('myApp.view1', ['ngRoute']) .config(['$routeProvider', function($routeProvider) { $routeProvider.when('/view1', { templateUrl: 'view1/view1.html', controller: 'V
angular.module('myApp.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'view1/view1.html',
controller: 'View1Ctrl'
});
}])
.controller('View1Ctrl', ['$scope', '$http',
function ($scope, $http) {
$http.get('/app/data/events.json').success(function(data) {
$scope.events = data.record;
}).error(function(data, status){
alert("error "+data+' | '+status);
});
$scope.orderProp = 'date';
}]);
p>Available Events</p>
<div ng-controller="EventListController">
<ul>
<li ng-repeat="event in events">
<p>{{event.name}}</p>
<div>{{event.location}}</div>
<div>{{event.description}}</div>
<div>{{event.date}}</div>
</li>
</ul>
<p>Total number of events: {{events.size}}</p>
</div>
p>可用事件
-
{{event.name}
{{event.location}
{{event.description}
{{event.date}
事件总数:{{events.size}
问题是:为什么不加载json文件?您应该使用如下$http方法:
// Simple GET request example:
$http.get('url').then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
代码数据
和状态
中的变量未定义
您应该像这样更新代码:
$http.get('/app/data/events.json')
.then(function successCallback(response) {
$scope.events = data.record;
},
function errorCallback(response) {
alert(response);
});
这将解决您的问题。打开控制台(F12)并检查,我认为问题在于您的“本地主机”。设置一些域名,然后再试一次。在Mac上使用Option+Command+J尝试从url中删除该应用程序,并按如下方式键入/data/events.json