【问题标题】:AngularJS ng-show not working with $timeout?AngularJS ng-show 不能与 $timeout 一起使用?
【发布时间】:2016-10-29 10:42:07
【问题描述】:

我想选中标签并显示 2 个按钮。

这是我的 aspx 代码:

 <button id="UxMoveAllRight" style="width: 40px; height: 25px; margin-top: 20px;" ng-show="showMoveallButtons()">
                                <i class="pi-icon pi-icon-angle-double-right"></i>
                            </button>
                            <button id="UxMoveAllLeft" style="width: 40px; height: 25px; margin-bottom: 20px;" ng-show="showMoveallButtons()">
                                <i class="pi-icon pi-icon-angle-double-left"></i>
                            </button>

这是我的 ng-show 功能代码(在 controller.js 中):

$scope.showMoveallButtons = function () {
                    var show = false;
                    $timeout(function () {
                        var activetab = $find("Add").get_selectedTab().get_name();
                        if (activetab == "AddBulk") { show = true }
                        else { show = false };
                    });
                    return show;
                };

但总是返回 false。 我想,如果选定的选项卡等于 AddBulk ,则显示 2 个按钮,否则不显示。

我猜,总是返回错误的原因:超时?

你知道我该如何解决吗?

【问题讨论】:

  • 在你的代码中 return show; 就像 return false; 总是
  • 什么意思?我在哪里做错了@Tushar
  • ng-show 指向范围属性并在超时内更改它。

标签: javascript asp.net angularjs timeout angularjs-ng-show


【解决方案1】:

在你的timeOut() 中使用$scope.show 并在你的指令ng-show. 中影响她

JS

$scope.showMoveallButtons = function () {
                    $timeout(function () {
                        var activetab = $find("Add").get_selectedTab().get_name();
                        if (activetab == "AddBulk") { $scope.show= true }
                        else { $scope.show= false };
                    },/*Your delay here*/);
                };

HTML

<button id="UxMoveAllRight" style="width: 40px; height: 25px; margin-top: 20px;" ng-show="show">

【讨论】:

    【解决方案2】:

    只需放入控制器($timeout)

    '使用严格';

    app.controller('ctrlname', ['$scope','$http','$timeout', function($scope,$http,$timeout){
    
            ......
            ......
    }]);
    

    【讨论】:

      【解决方案3】:

      试试这个:

      $超时(函数(){ var activetab = $find("Add").get_selectedTab().get_name(); if (activetab == "AddBulk") { show = true } 否则 { 显示 = 假 }; 回归秀; });

      【讨论】:

      • 这不是最好的方法,因为 $timeout() 是一个异步函数,$timeout 之后的代码总是会在它之前运行。
      猜你喜欢
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 2016-04-24
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      相关资源
      最近更新 更多