AngularJS: ng-placeholder not working

删除回忆录丶 提交于 2019-12-10 00:53:29

问题


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.


回答1:


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.)




回答2:


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.




回答3:


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;


来源:https://stackoverflow.com/questions/30573999/angularjs-ng-placeholder-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!