44

What is a simple way to change the text in a button element based on a boolean value?

pseudo code:

<button> some text OR some other text </button>

I read this: Angularjs if-then-else construction in expression

and this regarding ng-switch: http://docs.angularjs.org/api/ng.directive:ngSwitch

Neither seems to work using bool value from model

6 Answers 6

116

Should use like that :

<button> {{ cond_vall == true ? 'Case 1' : 'Case 2' }}</button>
Sign up to request clarification or add additional context in comments.

3 Comments

I'm having a weird spacing issue with this approach, see my question here stackoverflow.com/questions/35456600/ionic-weird-spacing
@RockOnGom: What's the point of comparing a boolean to true? This works just fine: {{ cond_vall ? 'Case 1' : 'Case 2' }}
Use cond_vall === true
15

I guess it depends on what the text is that you are trying to show. If you have control over what the text is in the controller, you can bind the text to a scope variable and set it in the controller so you don't have to put any logic in the view. Something like:

<button>{{someScopeVarWithYourString}}</button>

Otherwise, you can use an ng-if or ng-show on the boolean condition.

<button ng-show="someBoolValue">some text</button>
<button ng-show="!someBoolValue">some other text</button>

3 Comments

I ended up doing exactly what you mentioned in the first option. perhaps this is the cleanest approach. second approach toggles between two different buttons and I want to keep one button.
I agree. The "cleanest" approach is to do it from the model. and the UI view is just a "dump" reflection of the model. This is the idea behind separate model / view in the first place
Yea, it is. Keeps your view less cluttered also.
11

Because I needed to use a filter (translate filter) for the button text, the following solution worked best for me:

<button>
    <span> {{ condition ? 'some_text' : 'some_Other_text' | translate }}</span>
</button>

Comments

1

Only parenthesis seem required when combining Angular 8 LTS, Typescript 3.5 and ngx-bootstrap

<button type="button" class="btn"> {{ ( boolVar ? 'text' : 'other text')  | translate }} </button>

                                            

1 Comment

The question is for AngularJS not Angular
0
<div ng-app="myModule">
  <div ng-controller="myController">
    {{message}} <br />
    <button ng-click="changeMessage(message)">Change Message</button>
  </div>
</div>

var module = angular.module("myModule", []);
module.controller("myController", function($scope) {
$scope.message = "Hello World";
  $scope.changeMessage = function(value) {
  if(value==="Hello World"){
  $scope.message = "Hello Universe!";
  }else{
    $scope.message = "Hello World";
  }

  };
});

2 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
This doesn't answer the question at all
0
xyz.component.html

<button (click)="isCoreComponentUpdate ? onSubmit(): updateCoreComponent()">{{isCoreComponentUpdate ? 'Update' : 'Add'}}</button>
xyz.component.ts
isCoreComponentUpdate: Boolean= false;

when we want to use same button as Add and edit when we open add popup and edit popup that time this code will helpfull

where 'isCoreComponentUpdate' Boolean value based on that we are showing label and enabling action.

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.