我刚开始使用Angular,它似乎很好实现。我总是使用ng-model从textfield获取文本。我可以通过ng-model或其他工具获取按钮文本吗?例如,这是按钮
ng-model
textfield
<button class="common btnDarkGrey">Do it</button>
我想用Angular得到Do it。
Do it
tyu7yeag1#
您应该使用ng-bind将控制器中的数据绑定到按钮。
ng-bind
<button ng-bind="buttonNameVariable"></button>
然后,在控制器中,您将有一个名为scope.buttonNameVariable="Do it"的变量您可以在控制器中使用变量,并使用变量检索它。ng-model只用于html中的输入字段,而不是按钮。
scope.buttonNameVariable="Do it"
qhhrdooz2#
怎么样:
$scope.buttonText = 'Do It' //in the JS<button>{{buttonText}}</button> <!-- in the HTML -->
$scope.buttonText = 'Do It' //in the JS
<button>{{buttonText}}</button> <!-- in the HTML -->
或:
<button ng-init="buttonText = 'Do It'">{{buttonText}}</button>
然后在buttonText变量中有按钮的文本。
buttonText
zbdgwd5y3#
如果你不介意将jQuery代码放在angular中,可以这样做:
$('button.common').click(function(){ console.log($('button.common').html())});
$('button.common').click(function(){
console.log($('button.common').html())
});
3条答案
按热度按时间tyu7yeag1#
您应该使用
ng-bind
将控制器中的数据绑定到按钮。然后,在控制器中,您将有一个名为
scope.buttonNameVariable="Do it"
的变量您可以在控制器中使用变量,并使用变量检索它。ng-model只用于html中的输入字段,而不是按钮。
qhhrdooz2#
怎么样:
或:
然后在
buttonText
变量中有按钮的文本。zbdgwd5y3#
如果你不介意将jQuery代码放在angular中,可以这样做: