我想要的是在单击按钮并将其隐藏2秒后显示DIV。

<div ng-show="alertMsg.show">{{alertMsg.text}}</div>


触发click事件后,DIV可以正确显示,但无法隐藏。似乎“ alertMsg.show”的值在2秒后已正确更改为FALSE,但DIV仍在视图中。

点击事件:

$scope.$apply(function(){
            $scope.alertMsg.show = true;
        });

        $timeout(function () {
            $scope.alertMsg.show = false;
        }, 2000);


我想知道如何通过$ timeout隐藏DIV

最佳答案

隐藏有时间限制的节目

实时代码在这里http://jsfiddle.net/dofrk5kj/4/

的HTML

<div ng-controller="homeController">
        <button ng-click="showAlert()"> Show alert </button>
      <div ng-show="alertMsg.show">{{alertMsg.text}}</div>
</div>


controller.js

controller('homeController', function ($scope, $timeout) {
     $scope.alertMsg = {
        show: false,
        text:"Alert message is ::show"
     };
     $scope.showAlert =function(){
         $scope.alertMsg.show = true;
         $timeout(function(){
             $scope.alertMsg.show = false;
          }, 2000);
     }
   });


但是您可以使用单独的标志来隐藏/显示警报

<alert ng-repeat="alert in alerts" type="{{alert.type}}"
                    close="closeAlert($index)">{{alert.msg}} </alert>

关于javascript - ng-show在此示例中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29162718/

10-12 12:49
查看更多