简体   繁体   中英

Prevent a button from being clicked twice in angularjs

I am working in angularjs and am new to it, I have made an app using this.

There are a few pages in my app, now my problem is i am having two buttons which navigate to other pages, but my issue is when i click on a button (span), it opens the same page twice.

Can anyone please help me how to solve this? My code is as below:

javascript

  $scope.foo = function() {
        if ($scope.filterflg !== "1" ) {
            $scope.filterflg = "1";
             $scope.disabled = true;


            gallery.pushPage("filter.html", {
                params: FkCategory
            });

            $timeout(function() {
                console.log("====timeout occured===");
                $scope.filterflg = "0";
                  $scope.disabled = false;
            }, 3000);
        }
    };

html

<ons-button  id="test1" class="toolbar-button--quiet navigation-bar__line-height"  ng-click="isProcessing=true;foo()" ng-model ="filterflg"
         style="border: none; padding: 0 5px 0 0; margin-right:7px ;">
            <i class="ion-android-options" style="font-size:24px; color: #FFFFFF;"></i>
        </ons-button>

I have wasted 10 days on this issue, hope some buddy will help me to save my job..! :(

ng-disabled doesn't work on span. It only works for input or button types.

you have two options -

  1. convert the span to a button element

  2. use a variable like $scope.isProcessing to wrap the foo() function.

    $scope.foo = function() {

     if (!$scope.isProcessing) { //... $timeout(function() { console.log("====timeout occured==="); $scope.filterflg = "0"; $scope.disabled = false; $scope.isProcessing = false; }, 3000); } 

    };

html

<span ng-click="isProcessing=true;foo()"></span>

convert the span to a button element and disable that button without $timeout just after first click

 $scope.foo = function() {
        just simply 
        $scope.disabled = false;
        // Your other code 


 };

or use a flag to check page is already open or not will solve your problem

 $scope.isFilteropen = false;

 $scope.foo = function() {
     if ($scope.isFilteropen == false) {
         $scope.isFilteropen = true;
        // your other code 
     }
 };

Try this one,

<span class="toolbar-button--quiet navigation-bar__line-height" ng-disabled="disabled" ng-click="disabled=true;foo();disabled=false;" ng-model ="filterflg"> </span>

And remove your timer from the function Foo() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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