Html 如何使用AngularJS使指令与文本区域一起工作?
我有angularJS指令,根据下面的代码运行良好,但这里我有问题,它不允许我增加textarea行。我不知道为什么会发生这种情况。我花了几个小时,但无法找出问题。。我会很感激你的帮助 main.htmlHtml 如何使用AngularJS使指令与文本区域一起工作?,html,angularjs,angularjs-directive,Html,Angularjs,Angularjs Directive,我有angularJS指令,根据下面的代码运行良好,但这里我有问题,它不允许我增加textarea行。我不知道为什么会发生这种情况。我花了几个小时,但无法找出问题。。我会很感激你的帮助 main.html <div class="col-md-7"> <textarea rows="2" class="form-control" ng-model="processDTO.processStatementText" id="processS
<div class="col-md-7">
<textarea rows="2" class="form-control"
ng-model="processDTO.processStatementText"
id="processStatement" placeholder="Process statement" maxlength="4000"
chars-ct-tooltip-focus="{{processDTO.processStatementText}}"
required>
</textarea>
</div>
指令.js
angular.module('App').directive('charsCtTooltipFocus', function () {
'use strict';
return{
restrict: 'A',
template: '<input tooltip tooltip-placement="top" tooltip-trigger="focus">',
replace: true,
link: function (scope, element, attrs) {
attrs.$observe('charsCtTooltipFocus', function (value) {
var modelValue = value;
var maxlength = attrs.maxlength;
modelValue = maxlength-modelValue.length+' characters left';
attrs.$set('tooltip', modelValue);
});
}
};
});
angular.module('App').directive('charsCtTooltipFocus',function(){
"严格使用",;
返回{
限制:“A”,
模板:“”,
替换:正确,
链接:函数(范围、元素、属性){
属性$observe('charsCtTooltipFocus',函数(值){
var模型值=价值;
var maxlength=attrs.maxlength;
modelValue=maxlength modelValue.length+“左字符”;
属性$set('tooltip',modelValue);
});
}
};
});
我必须更改指令,因为它被设置为替换true,并且只支持输入字段,所以我实现了下面的指令,使它可以在textarea和input上工作
指令.js
angular.module('riskAssessmentApp').directive('characterCounter', function () {
'use strict';
return{
restrict: 'A',
require: '^ngModel',
link: function (scope, element, attrs, ngmodel) {
var characterCount;
element.after('<p class="character-count" style="display: none;"><span class="characters-left"></span> characters left</p>');
element.focus(function(){
element.next().show();
});
element.focusout(function(){
element.next().hide();
});
scope.$watch(function(){
return ngmodel.$viewValue;
}, function(newVal, oldVal){
if(newVal){
characterCount = parseInt(attrs.maxlength - newVal.length, 10);
} else{
characterCount = parseInt(attrs.maxlength, 10);
}
element.next().find('.characters-left').text(characterCount);
})
}
};
});
角度模块('riskAssessmentApp')。指令('characterCounter',函数(){
"严格使用",;
返回{
限制:“A”,
要求:“^ngModel”,
链接:功能(范围、元素、属性、模型){
var特征计数;
元素。在(“剩余字符之后,
”; 元素。焦点(函数(){ 元素。next().show(); }); 元素。focusout(函数(){ 元素。next().hide(); }); 范围$watch(函数(){ 返回ngmodel.$viewValue; },函数(newVal,oldVal){ if(newVal){ characterCount=parseInt(attrs.maxlength-newVal.length,10); }否则{ characterCount=parseInt(attrs.maxlength,10); } 元素.next().find('.characters left').text(characterCount); }) } }; });不让您增加行数是什么意思?您试图在代码中的何处更改它?是否尝试了类似element.attributes.rows=something@SunilD. 我想将textarea width设置为4行,因此使用此指令我无法实现它。或者您是否尝试将变量绑定到rows元素<代码>行=“{rowCount}}”显示您尝试过的代码。@SunilD。以上是我迄今为止尝试的所有代码