我正在尝试更新nav.html中的值
<nav class="navbar navbar-inverse" role="navigation" ng-controller="LoginController as login">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li><a href="#/foo">Show option '{{login.showOption.show}}' <span class="glyphicon glyphicon-info-sign"></span></a></li>
<li ng-if="login.showOption.show" dropdown>
<a href class="dropdown-toggle" dropdown-toggle>
Options <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a ng-href>1</a></li>
<li><a ng-href>2</a></li>
</ul>
</li>
</ul>
</div>
</nav>
{{login.showOption.show}}
指的是此控制器
'use strict';
(function (module) {
var LoginController = function (basicauth, currentUser, growl, loginRedirect, Option, $modal, $log) {
var model = this;
model.showOption = Option.value;
};
module.controller("LoginController", ['basicauth', 'currentUser', 'growl', 'loginRedirect', 'Option', '$modal', '$log', LoginController]);
}(angular.module("civApp")));
选项服务
'use strict';
(function (module) {
var option = function () {
var value = {
show: false
};
return {
value: value
};
};
module.service("Option", option);
}(angular.module("civApp")));
当hasAccess的值发生变化时,我希望nav.html也显示或不显示
'use strict';
(function (module) {
var GameController = function ($log, $routeParams, GameService, PlayerService, currentUser, Util, Option, $filter, ngTableParams, $scope, growl, $modal) {
var model = this;
$scope.$watch(function () {
return GameService.getGameById(model.gameId);
}, function (newVal) {
if (!newVal) {
return;
}
var game = newVal;
$scope.currentGame = game;
var hasAccess = game.player && game.player.username === model.user.username && game.active;
$scope.userHasAccess = hasAccess;
Option.value = {
show: hasAccess
};
//$scope.apply() <-- fails
return game;
});
};
module.controller("GameController",
["$log", "$routeParams", "GameService", "PlayerService", "currentUser", "Util", 'Option', "$filter", "ngTableParams", "$scope", "growl", "$modal", GameController]);
}(angular.module("civApp")));
但问题是没有任何反应。我认为这是因为我在手表里面,我需要使用申请或消化,但我不知道如何!
这是代码的链接
和
答案 0 :(得分:2)
我认为您的服务每次都返回false,您需要更改下面的服务。
还有一件事是你将服务编写模式与工厂模式(Reference link)混合在一起,这里的代码如下所示。
<强>服务强>
'use strict';
(function(module) {
var option = function() {
this.value = {
show: false
};
this.getValue = function() {
return this.value;
};
this.setShowValue = function(val) {
this.value.show = val;
};
};
module.service("Option", option);
}(angular.module("civApp")));
<强>控制器强>
Option.value = {
show: hasAccess
};
将替换为
Option.setShowValue(hasAccess);
在获取值对象时,您可以使用Option.getValue();
或Option.value
&安培;对于show
值,您可以执行Option.value.show
帮助你可以帮助你,谢谢: - )