【问题标题】:Dependent resolve object is returned as undefined in case of angularjs在 angularjs 的情况下,依赖的解析对象返回为未定义
【发布时间】:2020-05-13 09:35:29
【问题描述】:

我有一个场景,其中一个解析对象需要用作另一个解析对象的输入对象。 以下是代码示例:

在控制台中打印 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】:

    您没有正确地将 regionData 作为依赖注入。你停在 $rootScope。

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

    应该是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-16
      • 2014-02-04
      • 2015-04-17
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多