AngularJS: ng-placeholder not working
Asked Answered
C

3

20

I have the following input html element that I want to change the placeholder of depending on what is being held in my user model.

<input type="text" class="form-control" id="Username" name="Username" data-ng-model="user.Username" required="" ng-placeholder="{{user.AdAccount ? 'Username' : 'Ad Login'}}">

I even tried this method that is said to have worked in previous versions of angular, but no success.

ng-placeholder="user.AdAccount == true && 'Username' || 'AD Login'" 

At the moment my placeholder just appears completely blank. I also know that AdAccount is holding true/false correctly because it is being used elsewhere on the form with ng-show.

Coachwork answered 1/6, 2015 at 12:51 Comment(3)
which version of Angular you are usingBlitz
Have you tried the line you are using now without the {{}}?Oyez
Hi, I am using 1.3.15, which judging by an answer i saw elsewhere on here should be fine? And yes @Oyez I have tried it without the curly braces and it made no difference.Coachwork
C
57

I don't think there's any ngPlaceholder directive. Try changing your code to:

<input type="text" class="form-control" id="Username" name="Username" data-ng-model="user.Username" required="" placeholder="{{user.AdAccount ? 'Username' : 'Ad Login'}}" />

That is, change ng-placeholderinto just placeholder, and everything should work fine.

(Note also the self-closing slash if you need to conform to valid XHTML.)

Cacophonous answered 1/6, 2015 at 13:16 Comment(4)
I actually wasn't aware these conditional statements would work outside of the ng- directives. Thanks!Coachwork
{{ ... }} makes angular evaluate any given expression within the curly braces - try just {{ 2 + 2 }} or whatever, and you'll see. If you do use directives, you don't need the curlies.Cacophonous
Yeah seems obvious now you say it -_-Coachwork
Please not that this approach may not work in internet explorer. Cf. docs.angularjs.org/guide/ie You probably want to use ng-attr-placeholder instead.Nepil
B
15

You can try this:

<input type="text" class="form-control" id="Username" name="Username" data-ng-model="user.Username" required="" ng-attr-placeholder="{{user.AdAccount ? 'Username' : 'Ad Login'}}">

ng-attr- will work with any HTML attribute.

Between answered 31/8, 2016 at 11:44 Comment(0)
N
1

In HTML:

<input type="text" placeholder="{{doctorname ? doctorname : 'Dr. your name'}}"
       class="editable-class" ng-model="doctor.name" readonly/>

In controller:

$scope.doctorname = $scope.doctorinfo.name;
Nerin answered 17/11, 2015 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.