Does Anyone know a ip address mask plugin for AngularJS?
Because I tried to use the "Jquery Input IP Address Control", but it's not working. When I try to use it, the "ngModel" attribute doesn't get the value of the textfield. In the screen I can see the value inside the textfield, however, if I do ".value()" in the element, it return a "" value. The same thing occur when I see the value of the $scope element by using console.log().
Can anyone help me?
Thanks!
Edit: SOLVED
People, problem solved.
I used this directive available in http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController:
app.directive('contenteditable', function() {
return {
restrict: 'A', // only activate on element attribute
require: '?ngModel', // get a hold of NgModelController
link: function(scope, element, attrs, ngModel) {
if(!ngModel) return; // do nothing if no ng-model
// Specify how UI should be updated
ngModel.$render = function() {
element.html(ngModel.$viewValue || '');
};
// Listen for change events to enable binding
element.bind('blur keyup change', function() {
scope.$apply(read);
});
read(); // initialize
// Write data to the model
function read() {
ngModel.$setViewValue(element.val());
};
}
};
});
After I used this directive, the Jquery Plugin worked fine. Probably because now the ngNodel is getting the element.val(). Before, I think it was getting the element.text().
I'm just wondering why you need this in the first place. What about just using
[ngPattern][1]
directive andplaceholder
attribute ?Several notes:
commenters adding
^
and$
quantifiers to the regex. You don't need to do that since angular adding them for you insideng-pattern
directive (see angular.js source code for ng-pattern)I do not belive that regex is good match for checking if each number is in [0,255] range. What I would rather do is implement ng-ipaddress directive, working with
ngModelController
directly. (see js-fiddle or github link)jsfiddle github
Assuming @Jayesh answer and made some shortenings here is the resulting correct version with pattern for 0.0.0.0 through 255.255.255.255
I have added ng-model-options in order that validation be triggered only on blur effect so the error class if any only shows up after changing focus to another field. However it works only for AngularJS 1.3+