最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

I just started to work with Angular, it seems good to implement. I always use ng-model to get text from textfield. Can I get the button text with help of ng-model or something else?

For example this is button

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

and I want to get Do it with Angular.

I just started to work with Angular, it seems good to implement. I always use ng-model to get text from textfield. Can I get the button text with help of ng-model or something else?

For example this is button

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

and I want to get Do it with Angular.

Share Improve this question edited Aug 11, 2023 at 15:54 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jan 8, 2015 at 7:54 abdulbariabdulbari 6,2425 gold badges41 silver badges65 bronze badges 1
  • Why do you need to get the button text? Does it changed after it was initialized? button text is usually one way data-binding, only from model to view. – user3063182 Commented Jan 8, 2015 at 8:07
Add a ment  | 

3 Answers 3

Reset to default 2

You should use ng-bind to bind the data in your controller to the button.

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

Then in your controller you will have a variable named scope.buttonNameVariable="Do it"

You can use the variable in the controller, and retrieve it with the variable. You use ng-model only for input fields in html, not for buttons for example.

how about:

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

or:

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

Then you have the text of the button in the buttonText variable.

If you don't mind putting jQuery codes in angular, this could be done:

$('button.mon').click(function(){
   console.log($('button.mon').html())
});
发布评论

评论列表(0)

  1. 暂无评论