如何使用angularJS restful服务从外部文件使用json数据

如何使用angularJS restful服务从外部文件使用json数据,angularjs,Angularjs,我正在开发一个应用程序。因为我正在从一个 外部文件使用$http.get()方法,工作正常。现在我试着使用角度 restful服务。它在过滤器中工作得很好,但当我在控制器中使用它时,它就工作得很好了 显示未定义的 //Service.js File angular.module('calenderServices', ['ngResource']). factory('Events', function($resource){ return $resource('/EventCalender/:

我正在开发一个应用程序。因为我正在从一个

外部文件使用$http.get()方法,工作正常。现在我试着使用角度

restful服务。它在过滤器中工作得很好,但当我在控制器中使用它时,它就工作得很好了

显示未定义的

//Service.js File
angular.module('calenderServices', ['ngResource']).
factory('Events', function($resource){
return $resource('/EventCalender/:File.json', {}, {
query: {method:'GET', params:{File:'events'}, isArray:true}
});
});


        //This is my app Module
angular.module('calender', ['eventFilters','highlight','event','calenderServices']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
  when('', {templateUrl: 'template.html',   controller: MonthCtrl}).
  otherwise({redirectTo: '/invalid'});
}]);


     //This is my filter.js
angular.module('eventFilters', ['calenderServices']).filter('compare', function(Events) {
var events=Events.query();
alert(events[0].name); //it is displaying the name "Milestone1" });


     //This is my controller.
function MonthCtrl($scope, Events){
var events=Events.query();
    alert(events[0].name); //it is displaying undefined
    }

     //whenever i try to use the variable 'events' in controller, it is displaying undefined or null.  But in filter it is working fine. 

以下操作不起作用,因为ngResource发出异步http请求

var events=Events.query();
alert(events[0].name); // <--- here events is an empty array
它看起来像一个同步操作,但实际上不是。这是angular的禅在工作。你可以学英语

要进一步处理数据,还可以将成功回调传递给
get
方法

Events.query(function(events){
    // here you have access to the first event's name
    console.log(events[0].name);
});

这里有一个有效的例子:

我试过这个,但不起作用。我刚刚将名称“$scope.events”更改为“$scope.eve”,它正在工作。谢谢。
Events.query(function(events){
    // here you have access to the first event's name
    console.log(events[0].name);
});