Use jquery plugin within angular

136 Views Asked by At

how to use intl-tel-input(https://github.com/Bluefieldscom/intl-tel-input) jquery plugin in my angular app to format mobile number field.

1

There are 1 best solutions below

0
On BEST ANSWER

You need to load jQuery and wrap it in a directive.

Directive Def:

app.directive('intlTel', function(){
  return{
    replace:true,
    restrict: 'E',
    require: 'ngModel',
    template: '<input type="text" placeholder="e.g. +1 702 123 4567">',
    link: function(scope,element,attrs,ngModel){
      var read = function() {
        var inputValue = element.val();
        ngModel.$setViewValue(inputValue);
      }      
      element.intlTelInput({
        defaultCountry:'fr',
      });
      element.on('focus blur keyup change', function() {
          scope.$apply(read);
      });
      read();
    }
  }
}); 

Use:

<intl-tel ng-model="model.telnr"></intl-tel>

Watch this Plunker