admin管理员组文章数量:1289832
I'm trying to get the name of a button selected in a variable, and do something after (change the content of a <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>
?
I'm trying to get the name of a button selected in a variable, and do something after (change the content of a <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>
?
4 Answers
Reset to default 4You can use:
<button ng-click="actifSelected.select($event,1)"></button>
And in your controller, you can use $event to trigger element
$scope.actifSelected.select = function($event, value) {
var button = angular.element($event.currentTarget);
var buttonName = button.attr("name");
}
You can use the following expression:
<p>{{ actifSelected.selectedButton === 1 ? '1' : '2' }}</p>
You can pass $event to 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
Just display the selectedButton variable display in your
<p>{{actifSelected.selectedButton}}</p>
本文标签: javascriptGet button name in angularStack Overflow
版权声明:本文标题:javascript - Get button name in angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741402918a2376777.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论