我正在尝试获取在变量中选择的按钮的名称,然后执行一些操作(更改 <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>
<p>
如果选择信用卡按钮,则必须将“1”更改为“2”,我找到了ng-show
/ng-hide
但我想我可以简单地修改一个 <p>
的内容无需创建另一个 <p>
?
最佳答案
您可以使用:
<button ng-click="actifSelected.select($event,1)"></button>
在你的 Controller 中,你可以使用 $event 来触发元素
$scope.actifSelected.select = function($event, value) {
var button = angular.element($event.currentTarget);
var buttonName = button.attr("name");
}
关于javascript - 以 Angular 获取按钮名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35633816/