Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <textinput ng-model="TestValue" name="TestValue" text="Label Text" config="GetConfigurationForm()" ngx-ip-address required></textinput>
  2.  
  3. <div class="row">
  4. <div class="form-group" ng-class="{ 'has-error': IsInvalid() }">
  5. <label for="{{name}}" class="control-label">{{text}}</label>
  6. <input id="{{name}}" type="text" class="form-control" ng-model="ngModel" name="{{name}}" ngx-ip-address-or-name required>
  7. </div>
  8. </div>
  9.  
  10. var app = angular.module('test', []);
  11.  
  12. app.directive('ngxIpAddress', function()
  13. {
  14. return {
  15. restrict: 'A',
  16. require: 'ngModel',
  17. link: function(scope, element, attributes, ngModel)
  18. {
  19. ngModel.$validators.ngxIpAddress = function(modelValue, viewValue)
  20. {
  21. // Value being blank is OK
  22. if (ngModel.$isEmpty(modelValue))
  23. return true;
  24.  
  25. // If the string starts with a character then
  26. // this is not valid
  27. if (isNaN(parseInt(viewValue[0])))
  28. return false;
  29.  
  30. var blocks = viewValue.split(".");
  31. if(blocks.length === 4)
  32. {
  33. return blocks.every(function(block)
  34. {
  35. return parseInt(block, 10) >= 0 && parseInt(block, 10) <= 255;
  36. });
  37. }
  38.  
  39. console.log("valid = " + ngModel.$validators.ngxIpAddress);
  40. return false;
  41. };
  42. }
  43. };
  44. });
  45.  
  46. app.directive('textinput', function ()
  47. {
  48. return {
  49. restrict: 'E',
  50. scope: {
  51. //@ reads the attribute value, = provides two-way binding, & works with functions
  52. ngModel: '=',
  53. name: '@',
  54. text: '@',
  55. config: '&'
  56. },
  57. controller: function($scope) {
  58. $scope.IsInvalid = function()
  59. {
  60. var getConfigurationFunction = $scope.config();
  61. if (!getConfigurationFunction || !getConfigurationFunction[$scope.name])
  62. return false;
  63.  
  64. return getConfigurationFunction[$scope.name].$invalid;
  65. };
  66. },
  67. link: function(scope, element, attributes) {
  68. var inputElement = element.find("input");
  69. for (var attribute in attributes.$attr)
  70. {
  71. if (attribute !== "ngModel"
  72. && attribute !== "name"
  73. && attribute !== "text"
  74. && attribute !== "config")
  75. {
  76. inputElement.attr(attribute, attributes[attribute]);
  77. console.log(attribute);
  78. }
  79. }
  80. },
  81. template: '<div class="row">' +
  82. '<div class="form-group" ng-class="{ 'has-error': IsInvalid() }">' +
  83. '<label for="{{name}}" class="control-label">{{text}}</label>' +
  84. '<input id="{{name}}" type="text" class="form-control" ng-model="ngModel" name="{{name}}">' +
  85. '</div>' +
  86. '</div>'
  87. };
  88. });
  89.  
  90. app.controller(
  91. "TestController",
  92. [
  93. "$scope",
  94. function TestController(_scope)
  95. {
  96. _scope.TestValue = "TestTest";
  97. _scope.GetConfigurationForm = function()
  98. {
  99. return _scope.ConfigurationForm;
  100. };
  101. }
  102. ]
  103. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement