pass button id in ng-click angular.js

前端 未结 2 597
鱼传尺愫
鱼传尺愫 2021-02-13 12:50

I need to pass button id in Ionic Framework.

Here is what I have tried.

In js code:

angular.module(\'todo\', [\'ionic\'])
.contr         


        
相关标签:
2条回答
  • 2021-02-13 13:26

    Yo, check this gist:

    https://gist.github.com/lc-nyovchev/ed0a640a82a0f2dfd5a1

    That is a very easy and naive way to do it.

    <div data-ng-init="btnId='asd';">
        <button data-ng-attr-id="btnId" class="button button-light" data-ng-click="showAlert(btnId)">
            Click Me
        </button>
    </div>
    

    Or you can have in your controller:

    $scope.btnId = 'asd'; 
    

    Then you don't need the ng-init block div.

    Or you can get a handle to the $event in your ng-click, and get its target, and then get its id, but I wouldn't recommend that, it is not the angular way of doing things:

    <button id="bla" class="button button-light" data-ng-click="showAlert($event)">
        Click Me
    </button>
    
    
    $scope.showAlert = function(event){
        alert(event.target.id);
    }
    
    0 讨论(0)
  • 2021-02-13 13:27

    This works if no repeaters are there , if repeaters are the data attribute should have different names and secondly event.CurrentTarget.Id will make it work.

    0 讨论(0)
提交回复
热议问题