I am gonna get the count of letters in the textbox and show it in div tag which is featured by element directive .
我将在文本框中获取字母数,并以div标签显示,该标签由element指令表示。
<input type="text" ng-model="name">
<div counter-directive max-length="100" ng-model="name"></div>
div tag has to show something like this : 12/100 (12 is what we typed in input and 100 is the value of max-length )
div标签必须显示如下:12/100(12是我们输入的输入,100是max-length的值)
the problem is , I don't know how to get the value of max-length .
问题是,我不知道如何获得max-length的值。
here i have the example on jsfiddle
这里我有关于jsfiddle的例子
2 个解决方案
#1
3
Firsly, check your spelling. You've used lenght
a couple times in your question.
首先,检查你的拼写。你在问题中已经使用了几次。
You can get the max-length
attribute from the attrs
object passed into the link
function.
您可以从传递给链接函数的attrs对象中获取max-length属性。
link: function (scope, element, attrs) {
var foo = attrs.maxLength;
}
#2
1
You just have to do this:
你必须这样做:
app.directive('counterDirective', function () {
return {
restrict: 'A',
require: 'ngModel',
scope: { maxLength:'=', ngModel:'&' },
link: function (scope, element, attrs) {
console.log(scope.maxLength);
scope.$watch(scope.ngModel, function (value) {
if (value) {
var newValue = value.length;
console.log(value);
element[0].innerText = newValue;
}
});
}
}
});
});
I think you have to replace 'lenght' by 'length' :)
我认为你必须用'长度'来代替'lenght':)
#1
3
Firsly, check your spelling. You've used lenght
a couple times in your question.
首先,检查你的拼写。你在问题中已经使用了几次。
You can get the max-length
attribute from the attrs
object passed into the link
function.
您可以从传递给链接函数的attrs对象中获取max-length属性。
link: function (scope, element, attrs) {
var foo = attrs.maxLength;
}
#2
1
You just have to do this:
你必须这样做:
app.directive('counterDirective', function () {
return {
restrict: 'A',
require: 'ngModel',
scope: { maxLength:'=', ngModel:'&' },
link: function (scope, element, attrs) {
console.log(scope.maxLength);
scope.$watch(scope.ngModel, function (value) {
if (value) {
var newValue = value.length;
console.log(value);
element[0].innerText = newValue;
}
});
}
}
});
});
I think you have to replace 'lenght' by 'length' :)
我认为你必须用'长度'来代替'lenght':)