2

My ngModel is undefined when using the ngPattern directive. If I remove ngPattern, the ngModel works as expected. See plnkr.

Note how vm.condition does not show when typing a value in the text box, but as soon as the ngPattern is removed, vm.condition shows as expected.

HTML

<body ng-controller="MainCtrl as vm">
    <div ng-form="vm.frmTest">
      <input id="txtHealth" name="txtHealth" type="text" ng-model="vm.condition" ng-pattern="/^(.+)$/g">
      <div class="help-block" ng-messages="vm.frmTest.txtHealth.$error" ng-show="vm.frmTest.txtHealth.$invalid">
        <p ng-message="pattern">Error.</p>
      </div>
    </div>
    <span>{{vm.condition}}</span>
</body>

JS

app.controller('MainCtrl', function($scope) {
    this.condition = '';
});

NOTE: This is a dumbed down version showing the issue. My directive is showing the same issue.

2
  • You should use just pattern in this case Commented Nov 18, 2016 at 4:18
  • does my answer help your need?? Commented Nov 18, 2016 at 4:35

2 Answers 2

1

It is probably because of the pattern string that you are using in your directive. Try having a $scope bind and check your directive. You can check the relative PLUNKER example here:

Demo Example

HTML:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular-messages.js"></script>
        <script src="script.js"></script>
  </head>

  <body ng-controller="MainCtrl as vm">
    <div ng-form="vm.frmTest">
      <input id="txtHealth" name="txtHealth" type="text" ng-model="vm.condition" ng-pattern="re">
      <div class="help-block" ng-messages="vm.frmTest.txtHealth.$error" ng-show="vm.frmTest.txtHealth.$invalid">
        <p ng-message="pattern">Error.</p>
      </div>
    </div>
    <span>{{vm.condition}}</span>
  </body>

</html>  

JS:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  this.condition = '';
  $scope.re = /^(.+)$/g;
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you're going to use vm on the view then you will need to declare vm on your controller.

var vm = this;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.