本文介绍了NG-观点与决心展示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用决心以这种方式(似乎是做这样的标准方式)。但鉴于没有显示。任何想法我错过了什么?顺祝商祺
angular.module('fifaApp',['ngRoute'])
的.config(函数($ routeProvider){
$ routeProvider.when('/团队/:code',{
templateUrl:意见/ team_details.html',
控制器:'TeamDetailsCtrl为teamDetailsCtrl,
解析:{
AUTH:功能(UserService){
返回UserService.session();
}
}
});
}); .factory('UserService',['$ HTTP',函数($ HTTP){
VAR的服务= {
isLoggedIn:假的,
会议:函数(){
VAR承诺= $ http.get('/ API /会话)
promise.success(功能(响应){
service.isLoggedIn = TRUE;
返回响应;
});
返回的承诺;
};
退换货服务;
};
}]); .controller('MainCtrl',['$范围,权威性,
功能($范围,AUTH){
$ scope.auth = auth.response;
}]);
和HTML:
< DIV CLASS =团队的细节容器卡>
< DIV CLASS =队伍标志>
< IMG标题=图片提供:维基百科
NG-SRC ={{teamDetailsCtrl.team.logoUrl}}>
< / DIV>
< DIV CLASS =名>
<跨度NG绑定=teamDetailsCtrl.team.name>< / SPAN>
(小于跨度NG绑定=teamDetailsCtrl.team.fifa code>< / SPAN>)
< / DIV>
< DIV CLASS =细节>
< DIV CLASS =标签>
<跨度>&昵称LT; / SPAN>
< / DIV>
< DIV CLASS =标题>
<跨度NG绑定=teamDetailsCtrl.team.nickname>< / SPAN>
< / DIV>
< / DIV>
< DIV CLASS =细节>
< DIV CLASS =标签>
<跨度> FIFA排名及LT; / SPAN>
< / DIV>
< DIV CLASS =标题>
<跨度NG绑定=teamDetailsCtrl.team.fifaRanking>
< / SPAN>
< / DIV>
< / DIV>
< DIV CLASS =细节>
< DIV CLASS =标签>
<跨度>协会< / SPAN>
< / DIV>
< DIV CLASS =标题>
<跨度NG绑定=teamDetailsCtrl.team.association>< / SPAN>
< / DIV>
< / DIV>
< DIV CLASS =细节>
< DIV CLASS =标签>
<跨度>主教练< / SPAN>
< / DIV>
< DIV CLASS =标题>
<跨度NG绑定=teamDetailsCtrl.team.headCoach>< / SPAN>
< / DIV>
< / DIV>
< DIV CLASS =细节>
< DIV CLASS =标签>
<跨度>船长和LT; / SPAN>
< / DIV>
< DIV CLASS =标题>
<跨度NG绑定=teamDetailsCtrl.team.captain>< / SPAN>
< / DIV>
< / DIV>
< / DIV>
解决方案
添加 $ Q
服务。
.factory('UserService',['$ HTTP,$ Q',函数($ HTTP,$ Q){
VAR的服务= {
isLoggedIn:假的,
会议:函数(){
变种推迟= $ q.defer();
VAR承诺= $ http.get('/ API /会话)
promise.success(功能(响应){
service.isLoggedIn = TRUE;
deferred.resolve(响应);
}); 返回deferred.promise;
};
退换货服务;
};
}]);
I am using resolve in this way (seems to be the standard way to do this way). But the view is not showing. Any ideas what I missed? Best Regards
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;
}]);
And 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>
解决方案
Add $q
in service.
.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;
};
}]);
这篇关于NG-观点与决心展示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!