在angularjs的情况下,依赖的resolution对象会以未定义的方式返回。

问题描述 投票:0回答:1

我有一个场景,一个resolution对象需要被用作另一个resolution对象的输入对象。以下是代码示例。

当在控制台打印regionData时,我看到它被返回为未定义的数据

angular.module('myApp').config([
  '$stateProvider',
  '$urlRouterProvider',
  '$locationProvider',
  'ENV',
  function($stateProvider, $urlRouterProvider, $locationProvider, ENV) {
    if (ENV.html5Mode) {
      $locationProvider.html5Mode(true);
    }
    $locationProvider.hashPrefix('!');
    $urlRouterProvider.otherwise('/');
    $stateProvider.state('users', {
      url: '/users',
      views: {
        mainView: {
          templateUrl: '/modules/users/users.html',
          controller: 'UsersCtrl',
        },
      },
      resolve: {
        regionData: [
          'Dtahttp',
          function(Dtahttp) {
            return Dtahttp.retrieve('countrygroups');
          },
        ],
        articleListData: [
          'Dtahttp',
          'Dtauser',
          '$rootScope',
          function(Dtahttp, Dtauser, $rootScope, regionData) {
            return Dtauser.getProfile().then(function(profileData) {
              return Dtahttp.retrieve('location').then(function(locationData) {
                $rootScope.locationData = locationData;
                // Get countryCode data
                $rootScope.countryCode = locationData.countryCode;

                console.log(regionData);

                var postObj = {
                  countries: [],
                  tagIds: [],
                  pattern: '',
                  limit: 28,
                  offset: 0,
                };
                return Dtahttp.post('ListData', postObj);
              });
            });
          },
        ],
        language: [
          'Dtauilanguage',
          function(Dtauilanguage) {
            return Dtauilanguage.getLanguageData();
          },
        ],
      },
      onEnter: [
        'articleListData',
        function(articleListData) {
          var i;
          for (i = 0; i < articleListData.articles.length; i++) {
            var article = articleListData.articles[i];
            article.publishDateTime = moment(
              article.publishDateTime
            ).calendar();
            article.language = article.availableLanguages.filter(function(el) {
              return el.uuid == article.languageId;
            })[0];
          }
        },
      ],
    });
  },
]);

谁能帮我解决这个问题。

javascript angularjs angular-ui-router
1个回答
0
投票

你没有正确地注入regionData作为依赖关系。你停在了$rootScope。

articleListData: [
      'Dtahttp',
      'Dtauser',
      '$rootScope',
      function(Dtahttp, Dtauser, $rootScope, regionData)

应该是

articleListData: [
      'Dtahttp',
      'Dtauser',
      '$rootScope',
      ‘regionData’,
      function(Dtahttp, Dtauser, $rootScope, regionData)
© www.soinside.com 2019 - 2024. All rights reserved.