Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用指令时未定义ngModel-Angularjs控制器_Javascript_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

Javascript 使用指令时未定义ngModel-Angularjs控制器

Javascript 使用指令时未定义ngModel-Angularjs控制器,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,使用ngPattern指令时,我的ngModel未定义。如果删除ngPattern,ngModel将按预期工作。看 请注意,在文本框中键入值时,vm.condition如何不显示,但一旦删除ngPattern,vm.condition就会按预期显示 HTML 注意:这是一个显示问题的简化版本。我的指令显示了相同的问题。这可能是因为您在指令中使用的模式字符串。尝试绑定$scope并检查您的指令。您可以在此处查看相对PLUNKER示例: HTML: <!DOCTYPE html> &l

使用ngPattern指令时,我的ngModel未定义。如果删除ngPattern,ngModel将按预期工作。看

请注意,在文本框中键入值时,vm.condition如何不显示,但一旦删除ngPattern,vm.condition就会按预期显示

HTML


注意:这是一个显示问题的简化版本。我的指令显示了相同的问题。

这可能是因为您在指令中使用的模式字符串。尝试绑定
$scope
并检查您的指令。您可以在此处查看相对PLUNKER示例:

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>  
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  this.condition = '';
  $scope.re = /^(.+)$/g;
});

如果要在视图上使用vm,则需要在控制器上声明vm

var vm = this;

你应该只使用
模式
在这种情况下,我的回答是否有助于你的需要??
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  this.condition = '';
  $scope.re = /^(.+)$/g;
});
var vm = this;