ng-view没有显示出决心

时间:2015-10-25 16:22:05

标签: angularjs resolve

我以这种方式使用决心(似乎是这种方式的标准方式)。但这种观点没有表现出来。我错过了什么想法?最诚挚的问候

angular.module('fifaApp', ['ngRoute'])
  .config(function($routeProvider) {
   $routeProvider.when('/team/:code', {
      templateUrl: 'views/team_details.html',
      controller:'TeamDetailsCtrl as teamDetailsCtrl',
      resolve: {
        auth: function(UserService){
          return UserService.session();
        }
      }
    });
  });

  .factory('UserService', ['$http', function($http) {
    var service = {
      isLoggedIn: false,
      session: function() {
        var promise = $http.get('/api/session')
              promise.success(function(response) {
          service.isLoggedIn = true;
          return response;
        });
        return promise;
    };
    return service;
  };
  }]);

  .controller('MainCtrl', ['$scope','auth',
    function($scope,auth) {
      $scope.auth = auth.response;
  }]);

和html:

<div class="team-details-container card">
  <div class="team-logo">
      <img title="Image Courtesy: Wikipedia"
           ng-src="{{teamDetailsCtrl.team.logoUrl}}">
  </div>
  <div class="name">
      <span ng-bind="teamDetailsCtrl.team.name"></span>
      (<span ng-bind="teamDetailsCtrl.team.fifaCode"></span>)
  </div>
  <div class="detail">
    <div class="label">
      <span>Nickname</span>
    </div>
    <div class="title">
      <span ng-bind="teamDetailsCtrl.team.nickname"></span>
    </div>
  </div>
  <div class="detail">
    <div class="label">
      <span>FIFA Ranking</span>
    </div>
    <div class="title">
      <span ng-bind="teamDetailsCtrl.team.fifaRanking">
      </span>
    </div>
  </div>
  <div class="detail">
    <div class="label">
      <span>Association</span>
    </div>
    <div class="title">
      <span ng-bind="teamDetailsCtrl.team.association"></span>
    </div>
  </div>
  <div class="detail">
    <div class="label">
      <span>Head Coach</span>
    </div>
    <div class="title">
      <span ng-bind="teamDetailsCtrl.team.headCoach"></span>
    </div>
  </div>
  <div class="detail">
    <div class="label">
      <span>Captain</span>
    </div>
    <div class="title">
      <span ng-bind="teamDetailsCtrl.team.captain"></span>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

在服务中添加$q

.factory('UserService', ['$http' , '$q', function($http, $q) {
    var service = {
      isLoggedIn: false,
      session: function() {
        var deferred = $q.defer();
        var promise = $http.get('/api/session')
        promise.success(function(response) {
            service.isLoggedIn = true;
            deferred.resolve(response);
        });

        return deferred.promise;
    };
    return service;
  };
 }]);