Search code examples
angularjsangular-directive

Get value of attribute in Angularjs custom directive


I am gonna get the count of letters in the textbox and show it in div tag which is featured by element directive .

  <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 )

the problem is , I don't know how to get the value of max-length .

here i have the example on jsfiddle


Solution

  • 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.

    link: function (scope, element, attrs) {
        var foo = attrs.maxLength;
    }