AngularJS:ng-placeholder无法正常工作

use*_*039 17 html placeholder angularjs

我有以下输入html元素,我想根据我的用户模型中的内容更改占位符.

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

我甚至试过这种方法,据说在以前版本的角度有效,但没有成功.

ng-placeholder="user.AdAccount == true && 'Username' || 'AD Login'" 
Run Code Online (Sandbox Code Playgroud)

目前我的占位符显示为完全空白.我也知道AdAccount正确地保存了true/false,因为它正在使用ng-show在表单的其他地方使用.

Osk*_*erg 44

我认为没有任何ngPlaceholder指令.尝试将代码更改为:

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

也就是说,改变ng-placeholder为公正placeholder,一切都应该正常.

(如果需要符合有效的XHTML,请注意自动关闭斜杠.)


Hus*_*ain 12

你可以试试这个:

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

ng-attr-将适用于任何HTML属性.