title: Dynamic Model & Pattern with Regex
body: <p>I want to create a completely dynamic Form which is generated via ng-repeat and every input has its own ng-pattern(regex) values.</p>
  
  <p>Basically I got a string like this:</p>
  
  <pre><code>TestView.Txt.Num6_CLX.Txt3_ALB.Num3,2_CLX
  </code></pre>
  
  <p>So I would split the String to an array: </p>
  
  <p>My Filter</p>
  
  <pre><code>.filter('split', function () {
      return function (input, splitChar, splitIndex) {
          if (input != undefined &amp;&amp; input != null &amp;&amp; splitIndex) {
              return input.split(splitChar)[splitIndex];
          } else if (input != undefined &amp;&amp; input != null &amp;&amp; !splitIndex) {
              return input.split(splitChar);
          }
      };
  })
  </code></pre>
  
  <p>Then calling this in my controller</p>
  
  <pre><code>$scope.fields = $filter('split')($stateParams.fields, '.');
  </code></pre>
  
  <p>Leaving me this:</p>
  
  <pre><code>["TestView", "Txt", "Num6_CLX", "Txt3_ALB", "Num3,2_CLX"]
  </code></pre>
  
  <p>Now the First Part ("TestView") is the name of the View</p>
  
  <p>So I start my ng-repeat at index 1 like this:</p>
  
  <p>My Filter:</p>
  
  <pre><code>.filter('startFrom', function () {
      return function (input, start) {
          if (input) {
              start = +start; //parse to int
              return input.slice(start);
          }
          return [];
      }
  });
  </code></pre>
  
  <p>And the HTML:</p>
  
  <pre><code>&lt;div class="item item-input" ng-repeat="item in fields | startFrom : 1  track by $index"&gt;
  </code></pre>
  
  <p>So I have defined the  <code>$scope.values = {};</code> and now generate the Input Fields with the ng-repeat:</p>
  
  <p></p>
  
  <pre><code>&lt;input type="text" ng-enter="check(values, $index)" ng-model="values['field_'+$index]"/&gt;
  </code></pre>
  
  <p>Now when I trigger <code>ng-enter</code> with 1234 in field like this:</p>
  
  <pre><code>$scope.check = function (data, index) {
           console.log(data['field_' + index]);
           console.log($scope.values['field_' + index]);
  }
  </code></pre>
  
  <p>Result is both values are undefined... !</p>
  
  <p>The 2nd Problem is I want to match the entered Data with the dynamic Regex from the earlier splitted fields. <code>"Num3,2_CLX"</code> This part would mean it should be a max of 3digits before comma and max 2 after.</p>
  
  <p>First I split at the <code>'_'</code>, then I substring after 3 characters (field definition) to get the regex definition. And then I would <code>replace the x,y</code> values in the Regex string to match against the input value</p>
  
  <pre><code>var pattern = '/^(?!\.?$)\d{0,x}(\.\d{0,y})?$/g';
              var values = field.split('_');
              var numbers = values[0].substring(3);
              if (numbers.indexOf(',') &gt; -1) {
                  pattern.replace('x', numbers.split(',')[0]);
                  pattern.replace('y', numbers.split(',')[1]);
                  regex = new RegExp(pattern);
              } else {
                  pattern.replace('x', numbers);
                  pattern.replace('y', 0);
                  regex = new RegExp(pattern);
              }
              console.log(regex);
              console.log($scope.values['field_' + index].match(regex));
  </code></pre>