1

I want to show some text in html only when certain condition inside an input box is met. Otherwise, hide it. I am using AngularJS. Below is my html code.

  <form novalidate="" class="simple-form">conditional_number 
  <input type="text" ng-model="conditional_number " ng-change="search_number()"/>

If the conditional_number in the input box exceeds 5, I want the text Text to appear if condition is met to be displayed.

In AngularJS, there are ng-switch, ng-hide / ng-show, ng-if and perhaps more. I am at a loss which is the most appropriate. How can this be appropriately done in AngularJS?

3
  • Take a look at my answer, i've added a working example Commented May 8, 2014 at 9:41
  • 1
    @NidhishKrishnan: Thanks. The working example was very helpful! Commented May 8, 2014 at 9:43
  • @NidhishKrishnan: 2 more minutes before I can mark it as answer. Will do it as soon as I can. Commented May 8, 2014 at 9:47

2 Answers 2

1

you can simply achieve it using ng-show

Take a look at this

Working Example

 <form novalidate="" class="simple-form">
  <input name="myfield" ng-model="conditional_number" ng-minlength="5" ng-change="search_number()>
    <span ng-show="conditional_number.length>5">Good boy!</span>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

@RobH user3293156 has mentioned that he want text to be print when input text box length size exceeds 5. Its working fine
Oh I see. I thought "If the conditional_number in the input box exceeds 5" meant literally the number 5, not a 5 digit number.
1

I think ng-show is the most appropriate here:

<form novalidate="" class="simple-form">
  conditional_number 
  <input type="text" ng-model="conditional_number" ng-change="search_number()"/>
  <span ng-show="conditional_number> 5">Text to appear if condition is met</span>

This clearly communicates your intention to only show when the conditional number is greater than 5.

Here's a Plunker for you.

1 Comment

Thanks. That was very helpful too. I have upvoted but I can only choose one answer.

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.