我有这样的输入:
<input type="text" valid-text class="input-text" name="{{$ctrl.name}}">
关键是有效文本指令。此输入是一个组件,有时是有效文本,有时是有效数字。如何动态设置此值?
我测试了有效的 - {{$ ctrl.validType}}没有成功。
您可以从获得模型和属性的下一个指令开始。现在它添加属性string
和数字
myApp.directive('dynAttr', function() {
return {
scope: { list: '=dynAttr' },
require: 'ngModel',
link: function(scope, elem, attrs, ngModel){
for(attr in scope.list){
elem.attr(scope.list[attr].attr, scope.list[attr].value);
// you can change this logic
}
}
};
});
在你的控制器
$scope.dynAttribute = [
{ attr: 'number', value: '' },
{ attr: 'string', value: '' }
];
和HTML
<div ng-controller="MyCtrl">
<input ng-model="val" dyn-attr="dynAttribute"/>
</div>