简体   繁体   English

AngularJS-ng-show有条件不显示div标签吗?

[英]AngularJS - ng-show not showing div tag conditionally?

I am trying to show a div tag on a form based on the boolean flag that is specified in ng-show. 我试图在基于ng-show中指定的布尔标志的表单上显示div标签。 But the div is not displaying at all. 但是div根本没有显示。 Below is what I tried and it is not working. 以下是我尝试过的方法,它不起作用。 Thanks for any help! 谢谢你的帮助!

Form control in MVC View: MVC视图中的窗体控件:

<form name="payment" role="form" ng-submit="sendPayment()" ng-controller="PaymentCtrl">
        <div id="veil" ng-show="isProcessing == true"></div>
        <div id="feedLoading" ng-show="isProcessing == true">
            <img src="~/images/ajax_loader_blue_64.gif" class="img-responsive center-block" />
        </div>

        //Other form controls here
</form>

In Angular controller: This controller is same as the one the form is using which is "PaymentCtrl". 在Angular控制器中:此控制器与表单使用的控制器相同,即“ PaymentCtrl”。

$scope.isProcessing = false;

    $scope.setProcessing = function (loading) {
        $scope.isProcessing = loading;
    }

    $scope.sendPayment = function () {
        $scope.setProcessing(true);
        //Other logic here
        $scope.setProcessing(false);
    };

CSS Styles for div tags: div标签的CSS样式:

#veil {
position: absolute;
top: 0;
left: 0;
height:100%;
width:100%;
cursor: not-allowed;
filter: alpha(opacity=60);
opacity: 0.6;
background: #000000 url(http://www.wingo.com/angular/AngularShieldLogo.png) no-repeat center;
}

#feedLoading {
position: absolute;
top:200px;
width:100%;
text-align: center;
font-size: 4em;
color:white;
text-shadow: 2px 2px 2px #021124;
}

Your sendPayment is probably making an asynchronous service call which is waiting to be resolved via a promise. 您的sendPayment可能正在进行异步服务调用,正在等待通过sendPayment解决。 Your isProcessing boolean is being toggled immediately back at the end of the function's execution. 您的isProcessing布尔值将在函数执行结束时立即切换回去。 No matter when your promise actually resolves, your boolean has already been changed. 无论您的诺言何时真正解决,布尔值都已被更改。

You should instead reset your isProcessing boolean after your promise comes back. 相反,应在诺言再次出现后重置isProcessing布尔值。 Make sure to reset it in both the success and error callback functions. 确保在成功和错误回调函数中都将其重置。 For example, if your code is using a promise: 例如,如果您的代码使用了promise:

service.sendPayment() 
    .then(function() {
            $scope.setProcessing(false);
        },
        function(error) {
            $scope.setProcessing(false);
        });

Or if you are using $http in your controller, you can use the wrapped .success and .error callbacks: 或者,如果您在控制器中使用$http ,则可以使用包装的.success.error回调:

$http.post(url, data) 
    .success(function () {
        $scope.setProcessing(false);
    })
    .error(function (error) {
        $scope.setProcessing(false);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM