Skip to content

Instantly share code, notes, and snippets.

@weslley39
Created November 17, 2015 16:25
Show Gist options
  • Save weslley39/dc9ef9bc70f0a786b4b9 to your computer and use it in GitHub Desktop.
Save weslley39/dc9ef9bc70f0a786b4b9 to your computer and use it in GitHub Desktop.
"use strict"
angular.module("utils.maxlength", []).directive "rsMaxlength", ->
restrict: "A"
require: "ngModel"
scope:
model: "=ngModel"
link: (scope, elem, attrs, ctrl) ->
attrs.$set "ngTrim", "false"
maxlength = parseInt(attrs.rsMaxlength, 10)
unless angular.isDefined(attrs.hideInfo)
elementInfo = angular.element("<label>Restam <span id=\"qtdRestante\">" + (maxlength - _.size(scope.model)) + "</span> caracteres</label>")
elementInfo.insertAfter elem
ctrl.$parsers.push (value) ->
$("#qtdRestante").html maxlength - value.length
if value.length > maxlength
value = value.substr(0, maxlength)
ctrl.$setViewValue value
ctrl.$render()
value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment