Angularjs路由解析在加载控制器之前不填充模型

时间:2014-08-07 23:29:21

标签: angularjs angular-ui-router

我试图在我的控制器加载之前从后端(带标签和消息)填充我的模型。我的方法工作正常,它与后端连接并获取数据,但当我在控制器中查看该变量时,它是未定义的。我的变量是"模型"

这是我的路线档案

mainApp
    .config(["$routeProvider", function ($routeProvider) {
        .when(AngularRoutesFactory.AIMSAdmin.SearchBookings, {
              templateUrl: aimsAdminViewBase + "Bookings/SearchBookings.html",
              controller: "SearchPerioperativeBookingController",
              resolve: {
                  "Model": function (BookingFactory) {
                      return BookingFactory.GetSearchModel();
                  }
              },
              requireAIMSAuthorizeUser: true
          })
          .otherwise({
              redirectTo: AngularRoutesFactory.MainApp.BaseUrl
          });
    }]);

我的工厂

mainApp.factory("BookingFactory", ["$location", "MainFactory",
function ($location, MainFactory) {
 bookingsFactory.GetSearchModel = function () {
        bookingsFactory.MainFactory.QueryAPI(apiEndpoint + "GetSearchModel", "GET", function (response) {
            bookingsFactory.SearchBookingCriteria = response;
            return bookingsFactory.SearchBookingCriteria;
        }, null, null, bookingsFactory.LangInfo.Message_GettingBookingModel);
    }
 return bookingsFactory;


}]);

这是我的控制器

mainApp.controller("SearchBookingController", ["$scope", "BookingFactory", "$rootScope", "$location"
, function ($scope, BookingFactory, $rootScope, $location, Model) {
    $scope.bbb = Model;

}]);

2 个答案:

答案 0 :(得分:1)

编辑:

尝试以这种方式处理:

mainApp.config(["$routeProvider", "$q", function ($routeProvider, $q) {
    .when(AngularRoutesFactory.AIMSAdmin.SearchBookings, {
          templateUrl: aimsAdminViewBase + "Bookings/SearchBookings.html",
          controller: "SearchPerioperativeBookingController",
          resolve: {
              Model: function (BookingFactory, $q) {

                  var deferred = $q.defer();
                  BookingFactory.GetSearchModel().then(
                            function (data) {
                                deferred.resolve(data);
                            }, function () {
                                deferred.reject();
                            }
                        );
                  return deferred.promise;
              }
          },
          requireAIMSAuthorizeUser: true
      })
      .otherwise({
          redirectTo: AngularRoutesFactory.MainApp.BaseUrl
      });
}]);

答案 1 :(得分:0)

从@Fedaykin获得指导并提出以下工作解决方案。如果有错误请告诉我

我刚刚更改了我的工厂方法并通过应用$ q.defer方法解决了函数并​​使其正常工作

使用以下代码更改了我的工厂GetSearchModel方法

bookingsFactory.GetSearchModel = function () {
        bookingsFactory.MainFactory.QueryAPI(apiEndpoint + "GetSearchModel", "GET", function (response) {
            deferred.resolve(response);
        }, null, null, bookingsFactory.LangInfo.Message_GettingBookingModel);
        return deferred.promise;
    }

我在路线档案中做了什么

var bookingModel= function ($q, BookingFactory) {
    var deferred = $q.defer();
    BookingFactory.GetSearchModel().then(
              function (data) {
                  deferred.resolve(data);
              }, function () {
                  deferred.reject();
              }
          );
    return deferred.promise;
};
bookingModel.$inject = ["$q", "BookingFactory"];

然后解决我所做的一切

 .when(AngularRoutesFactory.AIMSAdmin.SearchBookings, {
              templateUrl: aimsAdminViewBase + "Bookings/SearchBookings.html",
              controller: "SearchBookingController",
              resolve: {
                  "Model": bookingModel
              },
              requireAIMSAuthorizeUser: true
          })

在控制器中我得到了值

mainApp.controller("SearchBookingController", ["$scope", "InitializeMainFactory", "$rootScope", "$location", "Model"
, function ($scope, InitializeMainFactory, $rootScope, $location, Model) {

    $scope.Model = Model;

}]);