Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <textinput ng-model="TestValue" name="TestValue" text="Label Text" config="GetConfigurationForm()" ngx-ip-address required></textinput>
- <div class="row">
- <div class="form-group" ng-class="{ 'has-error': IsInvalid() }">
- <label for="{{name}}" class="control-label">{{text}}</label>
- <input id="{{name}}" type="text" class="form-control" ng-model="ngModel" name="{{name}}" ngx-ip-address-or-name required>
- </div>
- </div>
- var app = angular.module('test', []);
- app.directive('ngxIpAddress', function()
- {
- return {
- restrict: 'A',
- require: 'ngModel',
- link: function(scope, element, attributes, ngModel)
- {
- ngModel.$validators.ngxIpAddress = function(modelValue, viewValue)
- {
- // Value being blank is OK
- if (ngModel.$isEmpty(modelValue))
- return true;
- // If the string starts with a character then
- // this is not valid
- if (isNaN(parseInt(viewValue[0])))
- return false;
- var blocks = viewValue.split(".");
- if(blocks.length === 4)
- {
- return blocks.every(function(block)
- {
- return parseInt(block, 10) >= 0 && parseInt(block, 10) <= 255;
- });
- }
- console.log("valid = " + ngModel.$validators.ngxIpAddress);
- return false;
- };
- }
- };
- });
- app.directive('textinput', function ()
- {
- return {
- restrict: 'E',
- scope: {
- //@ reads the attribute value, = provides two-way binding, & works with functions
- ngModel: '=',
- name: '@',
- text: '@',
- config: '&'
- },
- controller: function($scope) {
- $scope.IsInvalid = function()
- {
- var getConfigurationFunction = $scope.config();
- if (!getConfigurationFunction || !getConfigurationFunction[$scope.name])
- return false;
- return getConfigurationFunction[$scope.name].$invalid;
- };
- },
- link: function(scope, element, attributes) {
- var inputElement = element.find("input");
- for (var attribute in attributes.$attr)
- {
- if (attribute !== "ngModel"
- && attribute !== "name"
- && attribute !== "text"
- && attribute !== "config")
- {
- inputElement.attr(attribute, attributes[attribute]);
- console.log(attribute);
- }
- }
- },
- template: '<div class="row">' +
- '<div class="form-group" ng-class="{ 'has-error': IsInvalid() }">' +
- '<label for="{{name}}" class="control-label">{{text}}</label>' +
- '<input id="{{name}}" type="text" class="form-control" ng-model="ngModel" name="{{name}}">' +
- '</div>' +
- '</div>'
- };
- });
- app.controller(
- "TestController",
- [
- "$scope",
- function TestController(_scope)
- {
- _scope.TestValue = "TestTest";
- _scope.GetConfigurationForm = function()
- {
- return _scope.ConfigurationForm;
- };
- }
- ]
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement