Html AngularJS:ng 占位符不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30573999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS: ng-placeholder not working
提问by user3407039
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.
我有以下输入 html 元素,我想根据用户模型中保存的内容更改占位符。
<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.
我什至尝试过这种据说在以前版本的 angular 中有效的方法,但没有成功。
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.
目前我的占位符只是完全空白。我也知道 AdAccount 正确地持有真/假,因为它在表单上的其他地方与 ng-show 一起使用。
回答by Oskar Lindberg
I don't think there's any ngPlaceholder directive. Try changing your code to:
我认为没有任何 ngPlaceholder 指令。尝试将您的代码更改为:
<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-placeholder
into just placeholder
, and everything should work fine.
也就是说,更改ng-placeholder
为 just placeholder
,一切都应该正常工作。
(Note also the self-closing slash if you need to conform to valid XHTML.)
(如果您需要符合有效的 XHTML,还要注意自闭合斜线。)
回答by Hussain
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.
ng-attr-
将适用于任何 HTML 属性。
回答by Abhishek Saxena
In HTML:
在 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;