0

Data come from db , and this html element is a ng-repeat

{{count}}

My Question

How can i show if else condition that,

If count is null (nothing count value) I would show 0 (count value should be zero) , else show original count value

I hope there are solution like {{count if '' else ''}}

1
  • 3
    You can simply use {{count || 0}} Commented Sep 17, 2015 at 10:05

2 Answers 2

2

You could have have logic in your view with {{count || 0}} or {{ count ? count : 0 }} but this makes maintainability more difficult and duplicates logic.

A better option would be to create a custom filter. This will allow you to define you condition in a single place and makes it re-usable in many directives but also testable.

.filter('isEmpty', function() {
   return function (data) {
    return (data === null || data === undefined) ? 0 : data;   
   }
});

And you would use it like

{{item | isEmpty}}

See fiddle: http://jsfiddle.net/fhfsex4v/2/

EDIT: Here's the docs for filter that will explain what its doing and also how to pass optional extra parameters into your filters and other cool stuff. https://docs.angularjs.org/guide/filter

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

Comments

2

How about {{ count ? count : 0 }}

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.