简体   繁体   English

以角度获取按钮名称

[英]Get button name in angular

I'm trying to get the name of a button selected in a variable, and do something after (change the content of a <p> ). 我正在尝试获取在变量中选择的按钮的名称,然后执行某些操作(更改<p>的内容)。

<form>
    <button name="paypal" class="moyen-payement-button" ng-class="{actif: actifSelected.selectedButton === 1}" ng-click="actifSelected.select(1)">
        <img src="images/payement-methods/paypal.svg" alt="Paypal" class="paypal" />
    </button>
    <button name="creditCard" class="moyen-payement-button" ng-class="{actif: actifSelected.selectedButton === 2}" ng-click="actifSelected.select(2)">
         <img src="images/payement-methods/credit-card.svg" alt="Carte de crédit" class="creditCard" />
    </button>
</form>

<p>Name of my button: </p>
<p>1</p>

The <p> with "1" have to change in "2" if the button creditCard is selected, I've find ng-show / ng-hide but I think I can simply modify the content of a <p> without creating another <p> ? 如果选择了按钮creditCard,带有“1”的<p>必须在“2”中更改,我找到ng-show / ng-hide但我想我可以简单地修改<p>的内容而不创建另一个<p>

You can use: 您可以使用:

<button ng-click="actifSelected.select($event,1)"></button>

And in your controller, you can use $event to trigger element 在控制器中,您可以使用$ event来触发元素

$scope.actifSelected.select = function($event, value) {
    var button = angular.element($event.currentTarget);
    var buttonName = button.attr("name");
}

您可以使用以下表达式:

<p>{{ actifSelected.selectedButton === 1 ? '1' : '2' }}</p>

You can pass $event to ng-click: 您可以将$ event传递给ng-click:

  <button name="hello" ng-click="myFunction($event, 2)">Test</button>
  <p>{{btnName}}</p>
  <p>{{arg1}}</p>

With this in the controller: 在控制器中使用此功能:

$scope.myFunction = function(e, arg1) {
  $scope.arg1 = arg1;
  console.log(e.target.name);
  $scope.btnName = e.target.name;
}

See this Plunkr 看到这个Plunkr

只需在您的显示中显示selectedButton变量显示

 <p>{{actifSelected.selectedButton}}</p>

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

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