Thanks for all your answers.Your answers didn't me a give a proper solution.Then i have created a directive for that.
directive.js
myApp.directive('limitChar', function() {
'use strict';
return {
restrict: 'A',
scope: {
limit: '=limit',
ngModel: '=ngModel'
},
link: function(scope) {
scope.$watch('ngModel', function(newValue, oldValue) {
if (newValue) {
var length = newValue.toString().length;
if (length > scope.limit) {
scope.ngModel = oldValue;
}
}
});
}
};
})
html
<input type="text" limit-char limit="7" >
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…