从工厂取货时,控制器变量未定义

时间:2017-11-04 12:35:27

标签: javascript angularjs

如何在Angular中获取工厂结果的变量?当我试图从我的工厂得到结果时,我一直不明确。

例如在我的工厂我有

angular.module('MyApp.services', [])
.factory('ReportService', ['$http', '$window', '$upload', 'AuthService', function ($http, $window, $upload, AuthService) {
    return {
        findAll: function (criteria) {
            criteria = criteria || [];
            return $http.get(BASE_URL + '/ajax.php?action=reports.all&' + criteria.join('&'))
        },
        getAll: function (criteria){
         return $http.get(BASE_URL + '/ajax.php?action=reports.all&' + criteria.join('&'))
         .success(function(data, status) {
          return data;
          });
        }        }
}])

然后在我的控制器中

controller('MyController', ['$scope', '$compile', 'CartoDBService', 'ReportService', 'toaster', '$modal', '$rootScope', function ($scope, $compile, ReportService, toaster, $modal, $rootScope) {

     var reportSearchParams = ['graded=on'];

     var cat = ReportService.findAll(reportSearchParams);
     cat.then(function(data) {
         $scope.students = data.data;//don't forget "this" in the service
     });
     console.log($scope.students);//returns unidentified
    }

0 个答案:

没有答案
相关问题