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

时间:2016-04-24 04:24:46

标签: javascript angularjs

当我给ng-app命名并声明它时,为什么我的ng-show没有工作?

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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您需要为表单使用name属性,并使用该表单名称而不是模块名称myApp来验证表单字段。因此,请在表单标记name="myForm"中使用name属性,并使用myForm.email.$invalid

进行验证

像:

<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>

答案 1 :(得分:0)

您的代码段有语法错误,修复错误后的确切代码段正在运行。

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