在实例化控制器之前,Promise在路由中没有得到解决

时间:2014-10-01 14:05:59

标签: javascript angularjs

我正在尝试直接在路由中获取所需的数据,然后将要呈现视图的控制器实例化。

例如:

angular.module( 'mean.characters', ['ui.router', 'ui.bootstrap'] );

angular.module( 'mean.characters' ).factory('CharactersInfoFactory', ['$resource', function ($resource) {
  return $resource('/api/character/:characterId',
    { characterId: '@characterId' });
}]);

angular.module( 'mean.characters' ).config(['$stateProvider',
  function($stateProvider) {
    $stateProvider
        .state('bio', {
          url: '/:characterName/bio',
          templateUrl: 'characters/views/bio.html',
          resolve: {
                CharactersInfoFactory: 'CharactersInfoFactory',
                character: function(CharactersInfoFactory, $stateParams) {
                    var characterName = $stateParams.characterName;
                    return CharactersInfoFactory.get({characterId: characterName}).$promise;
                }
          },
          controller: 'CharactersBioController'
        });
  }
]);

angular.module( 'mean.characters' ).controller('CharactersBioController', ['$scope', 'Global', 'character',
  function($scope, Global, character) {
    $scope.global = Global;
    $scope.package = {
      name: 'characters'
    };

    $scope.character = character;

    console.log($scope.character);
  }

]);

数据检索得很好,但在呈现视图之前未解决承诺。

console.log在控制台中正确打印信息,但我也得到了下一个错误:

Error: [$injector:unpr] Unknown provider: characterProvider <- character

此外,视图不会打印信息(例如{{character.name}})。它只显示字符串“{{character.name}}”而不是名称本身。

我做错了什么?

提前致谢!

0 个答案:

没有答案