Is it possible to display alternate text in my div if {{user.userName}} is NULL?

In my text, I have the code:

<div>{{ user.userName }}</div> 

Is there a way to make this display a "Please Login" message if user.userName is NULL. Note. I would like to encode this business logic in the client, and not in the controller.

+6
source share
3 answers
 <div>{{ user.userName || 'alternate text' }}</div> <br> 

 <html> <head> <script data-require=" angular.js@ *" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"> </script> <link rel="stylesheet" href="style.css" /> </head> <body ng-controller="test"> <span>{{user.name || "alt text"}}</span> <script> var app = angular.module("app", []); app.controller("test", function($scope) {}); angular.bootstrap(document, ["app"]); </script> </body> </html> 
+8
source

You can do this in several ways:

 <div>{{ methodForTextDisplay(user.userName) }}</div> 

Or:

 <div>{{user.userName || "some default text"}}</div> 

Or:

 <div ng-show="user.userName" >{{ user.userName }}</div> <div ng-show="!user.userName" >Default text</div> 
+5
source
 <div ng-if="user.userName">{{user.userName}}</div> <div ng-if="!user.userName">Please login"</div> 
0
source

All Articles