1

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="common btnDarkGrey">Do it</button>

and I want to get Do it with Angular.

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. Commented Jan 8, 2015 at 8:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.