ng-view 没有显示决心
ng-view not showing with resolve
我正在以这种方式使用 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>
在服务中添加$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;
};
}]);
我正在以这种方式使用 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>
在服务中添加$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;
};
}]);