您可以使用ng-attr
指令动态设置占位符值,以检查表单元素pristine
是否需要将此字段放在form
& 中,然后您可以轻松检查该$pristine
表单元素的属性,例如myForm.price.$pristine
.
标记
<form name="myForm">
<input type="number" id="pricefield" min="0" name="price" ng-model="test.value"
ng-attr-placeholder="{{myForm.price.$pristine ? 'Some Value': 'Default Value'}}" required/>
</form>
演示 Plunkr
更新
我建议有一个指令来改变隐藏$viewValue
初始加载并显示占位符。指令将使用$formatters
on控制显示值ngModelControlller
标记
<input type="number" custom-dir id="pricefield" min="0" name="price"
ng-attr-placeholder="{{myForm.price.$pristine ? 'Initial Placeholder': 'After change Placeholder'}}"
ng-model="test.value" required/>
指示
app.directive('hideInitialLoad', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
var initalHide = true;
//format text going to user (model to view)
ngModel.$formatters.push(function(value) {
console.log(attr.min > value)
if (initalHide){
initalHide = false;
return undefined;
}
return value;
});
}
};
});
更新的演示