9
<div class="company_name" ng-controller="CompanyName">
 <h1 class="left">
     {{data.company_name}}
 </h1>
</div>

What I'd like to do is make it so that if data.company_name hasn't been added through an input field, it shows a placeholder "Company name", how can that be done using angularjs?

4 Answers 4

29

You can use ng-if and do something like

<div class="company_name" ng-controller="CompanyName">
 <h1 class="left">
     <span ng-if="data.company_name === ''">
       // Some placeholder
     </span>
     <span ng-if="data.company_name !== ''">
       {{data.company_name}}
     </span>
 </h1>
</div>

BTW ngIf is a new directive added in v1.1.5 so you might need to upgrade your angular version

See my plunker here : http://plnkr.co/edit/qiN2XshEpay6e6zzhUKP

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

2 Comments

That doesn't really work, the placeholder is still there and doesn't disappear when I start typing.
I added a Plunker to my answer for you to be able to test
1

One way to keep the code clean is to use a filter. This piece of code adds a class to an active tab.

var filters = angular.module('filters');
filters.filter('ie', function(){
  return function(v, yes, no){
      return v ? yes : no;
  };
});

Template

<li class="{{activeTab == 'home' | ie: 'active-class':''}}">
    Home
</li>

Comments

1

For Using ng-if, ng-else-if, and ng-else in your project use this:

https://github.com/zachsnow/ng-elif

Comments

0

You can use ng-if condition for the check company name vlaue. Let's take example of

 <span ng-if="driver.status_flag == 1">
    <i ngif="{{driver.status_flag}}" class="icon-ok-sign icon-2x link" style="color:#090" href="#" title="Payment received" ></i>
 </span>

In above example I have added condition status_flag value is 1 then the inside span value will show. Same way with your case you can add statement like

<span ng-if="data.company_name === ''">
        <i ngif="{{driver.status_flag}}" class="icon-ok-sign icon-2x link" style="color:#090" href="#" title="Payment received" ></i>
     </span>

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.