【问题标题】:AngularJS - Update another value inside watchAngularJS - 更新手表内的另一个值
【发布时间】:2015-03-30 17:05:11
【问题描述】:

我正在尝试更新我的 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")));

Option 服务

'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")));

但问题是什么都没有发生。我想是因为我在手表里面,我需要使用 apply 或 digest,但我不知道怎么做!

这里是代码链接

代码在这里: https://github.com/cash1981/civilization-boardgame/blob/feature/options/civilization-web/app/scripts/controllers/GameController.js

https://github.com/cash1981/civilization-boardgame/blob/feature/options/civilization-web/app/scripts/controllers/LoginController.js

https://github.com/cash1981/civilization-boardgame/blob/feature/options/civilization-web/app/views/nav.html

【问题讨论】:

    标签: angularjs angularjs-scope angularjs-watch


    【解决方案1】:

    我认为您的服务每次都返回 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

    帮助这可以帮助你,谢谢:-)

    【讨论】:

    • 我希望这行得通,但事实并非如此。即使我在 GameController.js 中明确设置 Option.setShowValue(true);,我的 nav.html 仍然显示 {{login.showOption.show}} 为 false
    • 没什么。你必须为此爱上 Angular :(
    • @ShervinAsgari 你应该添加这一行 model.Option = Option;'in controller &amp; on view like
    • ` 或 &lt;li ng-if="login.Option.getShowValue()" dropdown&gt;
  • 谢谢!终于做到了!我不明白为什么:)
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签