简体   繁体   English

具有相同模型的隐藏div中的angularjs ng-show延迟

[英]angularjs ng-show delay in hidden div with same model

My code is given below: 我的代码如下:

<button class="btn-border-blue" ng-show="!hasCouponCode" ng-click="hasCouponCode = true;">Cupom</button>
<div class="form-inline-cupom" ng-show="hasCouponCode">
    <input type="text" ng-model="couponCode" placeholder="Código do cupom">
    <button class="btn-green" ng-click="addCoupon()">Add cupom</button>
</div>

When I click on the button, it changes the model value "hasCouponCode", hides the button and shows the div element, but there is delay and visual is bad. 当我单击按钮时,它更改了模型值“ hasCouponCode”,隐藏了按钮并显示了div元素,但存在延迟,视觉效果很差。

Is there any way to fix this issue? 有什么办法可以解决此问题?

Thanks! 谢谢!

I created this snippet and it runs pretty quickly. 我创建了此代码段,它运行很快。 I think you might have a server callback in the addCoupon() method that's slowing it down. 我认为您可能在addCoupon()方法中有一个服务器回调,这会使它变慢。 You should improve the performance of the server side code. 您应该提高服务器端代码的性能。 If you can't, it'd be nice to add a loading screen to the view so the user knows something is working while waiting. 如果不能,最好将加载屏幕添加到视图中,以便用户在等待时知道有什么工作。 I hope it helped. 希望对您有所帮助。

 (function(angular) { var app = angular.module('myApp', []); var Maincontroller = function($scope) { $scope.addCoupon = function() { $scope.mycoupon = $scope.couponCode; } }; app.controller("maincontroller", ["$scope", Maincontroller]); }(angular)); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="maincontroller"> <button class="btn-border-blue" ng-show="!hasCouponCode" ng-click="hasCouponCode = true;">Cupom</button> <div class="form-inline-cupom" ng-show="hasCouponCode"> <input type="text" ng-model="couponCode" placeholder="Código do cupom"> <button class="btn-green" ng-click="addCoupon()">Add cupom</button> </div> <p>{{mycoupon}}</p> </div> </div> 

The problem can be solved by surrounding your button with an external div, and then using the ng-show in that div. 可以通过以下方法解决该问题:将您的按钮环绕在一个外部div上,然后在该div中使用ng-show。 Like this : 像这样 :

<div ng-show="!hasCouponCode">
<button class="btn-border-blue" ng-click="hasCouponCode = true;">Cupom</button>
</div>

Like this, you can apply it to rest of your code too. 这样,您也可以将其应用于其余代码。

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

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