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.
- 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
3 Answers
Reset to default 2You 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())
});