I'm currently trying to create a custom directive that initializes input with the following library: intl-tel-input .
So, I downloaded the required .js file using bower:
<script src="bower_components/intl-tel-input/build/js/intlTelInput.min.js"></script>
Then I create my input:
input type="tel" class="form-control" id="tel" name="tel" ng-model="informations.tel" ng-keyup="checkPhoneFormat()" ng-click="checkPhoneFormat()">
And I initialize it at the beginning of my controller as follows:
angular.element('#tel).intlTelInput({ validationScript: "../../bower_components/intl-tel-input/lib/libphonenumber/build/isValidNumber.js", preferredCountries: ['en', 'fr'] });
My problem is that when I try to access the informations.tel model, it is always undefined. The input does not seem to update the model value on the fly.
So I need to write something like this in order to relate the actual value of my input field to my immutable model value:
$scope.checkPhoneFormat = function(){ $scope.informations.telephone = angular.element('#telephone').val(); ...}
This may be fine, but I would like to create a custom directive to initialize such inputs, for example:
app.directive('phoneInput', function (PhoneFactory) { return { require: 'ngModel', restrict: 'A', scope: { phoneNumber: '=' }, link: function (scope, element, attrs, ctrl) { element.intlTelInput({ validationScript: "../../bower_components/intl-tel-input/lib/libphonenumber/build/isValidNumber.js", preferredCountries: ['en', 'fr'] }); ctrl.$parsers.unshift(function(viewValue) { console.log(viewValue); }); } }; });
But since the ngModel undefined initialization function is never reached ... Do you have any idea how I can solve my problem?