Angularjs 使用角度指令更改属性的值
所以我想要的是为输入的maxlength属性传递一个变量Angularjs 使用角度指令更改属性的值,angularjs,Angularjs,所以我想要的是为输入的maxlength属性传递一个变量 <input my-dir="maxLenght" id="inputScannedCode" type="text" ng-model="ScannedCode" maxlength="20" class="form-control orange-shadow" required placeholder=" Scan the code " /> 因此,我希望输
<input my-dir="maxLenght" id="inputScannedCode"
type="text" ng-model="ScannedCode" maxlength="20"
class="form-control orange-shadow"
required placeholder=" Scan the code " />
因此,我希望输入的长度从一个常数设置为一致性
因此,我创建了一个自定义指令,以便它更改maxlength值
.directive('myDir', function () {
return {
restrict: 'A',
scope: {
myDir: '='
},
template: '<div>{{myindex}}</div>',
link: function (scope, element, attrs) {
//console.log(attrs);
//attrs.maxlength = scope.myDir.toString();
//attrs.placeholder = scope.myDir.toString();
}
};
})
指令('myDir',函数(){
返回{
限制:“A”,
范围:{
myDir:“=”
},
模板:“{myindex}}”,
链接:函数(范围、元素、属性){
//控制台日志(attrs);
//attrs.maxlength=scope.myDir.toString();
//attrs.placeholder=scope.myDir.toString();
}
};
})
实际上,这会更改我的maxlength值,但编译后会更改,但当我检查元素时,它仍然显示maxlengh=“20”
你知道我需要做什么吗
<input my-dir="maxLenght" ng-maxlength="maxLength" id="inputScannedCode" type="text" ng-model="ScannedCode" class="form-control orange-shadow" required placeholder=" Scan the code " />
<input my-dir="maxLenght" id="inputScannedCode"
type="text" ng-model="ScannedCode"
maxlength="{{maxLenght || '20'}}"
class="form-control orange-shadow"
required placeholder=" Scan the code " />
$compile服务将创建一个监视程序,每次角度表达式更改时更新maxlength属性。问题在于,在呈现HTML页面后,maxlength被设置为(比方说)5,因此atribute保留其原始值(逻辑…)。我不想使用ng maxlength,因为这只会使嵌套输入的表单无效,但它确实允许用户超过设置的字符数。