【问题标题】:Resolve angular trouble [duplicate]解决角度问题[重复]
【发布时间】:2015-10-26 11:18:18
【问题描述】:

我按照这个例子,关于如何使用resolvehttp://blog.brunoscopelliti.com/show-route-only-after-all-promises-are-resolved/

但它不起作用,视图没有显示。以下是我使用的代码的基本部分:

angular.module('fifaApp', ['ngRoute'])
  .config(function($routeProvider) {

    $routeProvider.when('/', {
      templateUrl: 'views/team_list.html',
      controller: 'TeamListCtrl as teamListCtrl',
      resolve: {
        teams: function (FifaService) {
          return FifaService.getTeams();
      }
    }
    })
//For each route, we can decide to include the controller directly
//in the HTML or using the controller configuration in the route.
    .when('/login', {
      templateUrl: 'views/login.html'
    })
    .when('/team/:code', {
      templateUrl: 'views/team_details.html',
      controller:'TeamDetailsCtrl as teamDetailsCtrl',
      resolve: {
        //The auth resolve function returns a promise.
        auth: ['$q', '$location', 'UserService',
          function($q, $location, UserService) {
             return UserService.session().then(
               function(success) {},
               function(err) {
                  $location.path('/login');
                  $location.replace();
//reject the promise in the case of an error,
// because we still want the promise to fail.
//If we don’t $q.reject, 
//that tells AngularJS that the error was handled successfully                  
                  return  $q.reject(err);
             });
        }]
      }
    });
    $routeProvider.otherwise({
      redirectTo: '/'
    });
  });

angular.module('fifaApp')
  .factory('FifaService', ['$http',
    function($http) {
      var sdo = {
        getTeams: function() {
          var promise = return $http.get('/api/team');
          promise.success(function(response) {
            return response;
          });
          return promise;
      },

        getTeamDetails: function(code) {
          var promise = return $http.get('/api/team/' + code);
          promise.success(function(response){
            return response;
          });
          return promise;
        }
      }
      return sdo;
  }])

.controller('TeamListCtrl', ['FifaService','teams',
    function(FifaService,teams) {
      var self = this;
      self.teams = teams.response;

  }])

我错过了什么吗? 最好的问候

【问题讨论】:

  • But it does not work, the view is not showing. - 如果可能,请更具体并显示堆栈跟踪/错误
  • 几小时前你不是问过same question吗?
  • @zeroflagL 我不这么认为,因为我试图按照链接中的特定示例进行操作

标签: angularjs promise resolve


【解决方案1】:

也许使用支持 javascript 语法的 IDE。你有一些小故障...

angular.module('fifaApp', ['ngRoute'])
  .config(function($routeProvider) {

    $routeProvider.when('/', {
      templateUrl: 'views/team_list.html',
      controller: 'TeamListCtrl as teamListCtrl',
      resolve: {
        teams: function(FifaService) {
          return FifaService.getTeams();
        }
      }
    }) 
  }); // <-- missing brackets

angular.module('fifaApp')
  .factory('FifaService', ['$http',
    function($http) {
      var sdo = {
        getTeams: function() {
          var promise = $http.get('/api/team'); // <-- remove return statement
          promise.success(function(response) {
            return response;
          });
          return promise;
        },

        getTeamDetails: function(code) {
          var promise = $http.get('/api/team/' + code); // <-- remove return statement
          promise.success(function(response) {
            return response;
          });
          return promise;
        }
      }
      return sdo;
    }
  ])

.controller('TeamListCtrl', ['FifaService', 'teams',
  function(FifaService, teams) {
    var self = this;
    self.teams = teams.response;

  }
]);

【讨论】:

  • 我写道,我只发布了代码的相关部分。我更新了config 部分,现在是完整部分。抱歉,不清楚
  • 也许您应该阅读整个答案!您需要删除 return 语句。见plnkr.co/edit/5PRorPjFpOK21PJHG2TL?p=preview
  • 我想我发现了问题所在。 getTeamDetails 在工厂中...如果我从工厂中删除它,那么它就可以工作。否则,它不会。
  • 如果你一直无视答案,为什么还要寻求帮助?
  • 对不起,你误会了。您的回答有所帮助,现已被接受!非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2020-05-31
  • 2020-03-02
  • 2023-03-23
  • 2015-10-22
  • 2015-05-12
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多