0

我想在我的控制器中预加载数据。我resolve在 routeprovider 中使用:

.when('/customers', {
    controller: 'CustomerController', templateUrl: '/Customer/Index', resolve: {
        countries: CustomerController.loadCountries,
        genders: CustomerController.loadGenders,
    }
})

如您所见,我有两个对象将被注入到我的控制器中,国家和性别。这一切都很好。

我想要做的是,我希望这些对象成为一个对象的一部分:listData。我试过了:

.when('/customers', {
    controller: 'CustomerController', templateUrl: '/Customer/Index', resolve: {
        listData: {
            countries: CustomerController.loadCountries,
            genders: CustomerController.loadGenders
        }
    }
})

但这不起作用:Argument 'fn' is not a function, got Object

实现此目的的正确语法/方法是什么?

4

1 回答 1

1

如果将对象传递给键,则必须有一个函数作为值:

listData: function () {
  // you need to inject 
  var deferred = $q.defer();
  var listData = {};
  // these should be services, btw !
  CustomerController.loadCountries.then(function (countries) {
    listData.countries = countries;
    // resolve when you have both
    if (listData.genders) deferred.resolve(listData);
  });
  CustomerController.loadGenders.then(function (genders) {
    listData.genders = genders;
    if (listData.countries) deferred.resolve(listData);
  });
  return deferred.promise;
}
于 2013-06-20T10:20:35.257 回答