简体   繁体   English

angularJS:ng-show不适用于ng-app?

[英]angularJS: ng-show doesn't work with ng-app?

Why doesn't my ng-show work when I give ng-app a name and declare it? 给ng-app命名并声明它时,为什么ng-show无法正常工作?

https://jsfiddle.net/jzhang172/f7mrkf48/ https://jsfiddle.net/jzhang172/f7mrkf48/

 $(function(){ var app = angular.module('myApp', []); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <div ng-app="myApp"> <form name="formMe"> 1<input type="text"><br> 2<input type="text"><br> Email<input name="email" ng-model="email" required> <span ng-show="formMe.email.$touched">You touched me</span><br> <input type="submit" ng-disabled="formMe.email.$invalid"> </form> </div> 

You need to use name attribute for your form and use that form name instead of module name myApp to validation form field. 您需要为表单使用name属性,并使用该表单名称而不是模块名称myApp来验证表单字段。 So use name property in your form tag name="myForm" and for validation use myForm.email.$invalid 因此,请在表单标签name="myForm"使用name属性,并使用myForm.email.$invalid进行验证myForm.email.$invalid

like: 喜欢:

<form name="myForm">

    1<input type="text"><br>
    2<input type="text"><br>
    Email<input type="email" name="email" ng-model="email" required>

    <span ng-show="myForm.email.$touched">You touched me</span><br>
   <input type="submit" ng-disabled="myForm.email.$invalid">
</form>

Your code snippet has syntax error, the exact code snippet after fixing the error is working. 您的代码段存在语法错误,更正错误后的确切代码段可以正常工作。

 var app = angular.module('myApp', []); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <div ng-app="myApp"> <form name="formMe"> 1 <input type="text"> <br>2 <input type="text"> <br>Email <input name="email" ng-model="email" required> <span ng-show="formMe.email.$touched">You touched me</span> <br> <input type="submit" ng-disabled="formMe.email.$invalid"> </form> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM