如何在角度.js中使用ng模型获取按钮文本

How to get button text with ng-model in angular.js?

本文关键字:ng 模型 获取 文本 按钮 js      更新时间:2023-09-26

我刚刚开始使用Angular,实现起来似乎很好。我总是使用ng-modeltextfield获取文本。我可以在ng-model或其他东西的帮助下获取按钮文本吗?

例如,这是按钮

<button class="common btnDarkGrey">Do it</button>

我想和Angular Do it

应使用 ng-bind 将控制器中的数据绑定到按钮。

<button ng-bind="buttonNameVariable"></button>    

然后在控制器中,您将有一个名为 scope.buttonNameVariable="Do it" 的变量

您可以在控制器中使用该变量,并使用变量检索它。例如,您仅将ng-model用于html中的输入字段,而不用于按钮。

怎么样:

$scope.buttonText = 'Do It' //in the JS
<button>{{buttonText}}</button> <!-- in the HTML -->

或:

<button ng-init="buttonText = 'Do It'">{{buttonText}}</button>

然后,在 buttonText 变量中具有按钮的文本。

如果你不介意把jQuery代码放在角度中,这可以做到:

$('button.common').click(function(){
   console.log($('button.common').html())
});