1

I am reading data from a JSON string using angular and playing around with my app at the moment.

Task Status : {{task.completed}}  // displays true or false

I want to achieve something like this:

if(task.completed == true) print "completed";
else "print Not completed"

How can this be done in an angular expression?

2
  • You can use ng-if to get this done.... Commented Aug 19, 2016 at 12:33
  • angular 1 or 2? you could have an Angular2 <element *ngIf="task.completed"> or *ngIf="!task.completed". Or, for Angular 1, you could have ng-if="task.completed"/ng-if="!task.completed" Commented Aug 19, 2016 at 12:37

3 Answers 3

7

You can use conditional operator in view like

Task Status : {{task.completed ? "completed" : "print Not completed"}} 
Sign up to request clarification or add additional context in comments.

Comments

3

You can use ng-if or ng-show for it

<div> Task Status : 
 <span ng-if="task.completed">{{"completed"}}</span 
 <span ng-if="!task.completed">{{"print Not completed"}}</span 
</div>

Also @Satpal answer is really helpfull

Comments

0

If print means simply showing a label then this can be done like this example below..

    <div ng-app="myApp">
        <div ng-controller="myCtrl">
          {{names}}
            <ul>
              <li ng-repeat="x in names">
                {{ x.Name + ', ' + x.Country }}
              </li>
            </ul>
        </div>    
    </div>

Js code will be

    var myApp = angular.module('myApp', []);

    var myCtrl = function ($scope,$http) {
           $http.get("http://www.w3schools.com//website/Customers_JSON.php")
      .success(function(response) {$scope.names = response;});
    }

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.