2016-11-05 47 views
0

我要的是这样的: https://github.com/PatrickO10/meetUp如何显示一些通知,当输入不正确,并隐藏在输入正确

当你按下注册,你可以看到一些红色的音符。

他的代码在这里: https://github.com/PatrickO10/meetUp/blob/master/index.html#L95-L106 我试图自己写,但一直失败,不知道原因。我是这个领域的新人,你能帮我写吗?谢谢! 这是我简单的代码来测试这个部分: https://plnkr.co/edit/OL1QuesgZpqeEKoYS5cF?p=preview

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 




</head> 
<body> 
    <form name=signUpForm> 
    <label for="name"> 
      <span>name:</span> 
      <input type="text" id="nameInput" name="nameInput" class="form-control" placeholder="your name" required autocomplete="name" autofocus ng-model="nameModel"> 
       <div class="input-error" ng-message="signUpForm.nameInput.$error" role="alert" ng-if="signUpForm.nameInput.$touched"> 
        <div ng-message="required"> 
         <p>Input your name please!</p> 
        </div> 
       </div> 

     </label> 
    </form> 

</body> 
</html> 
+0

将ng-app,ng-controller放置在您的模板中。没有ng-app,angular将不会引导你的模板。 ng-model,ng-if只能在角度引导应用程序之后才起作用 –

+0

但是在我添加ng-app之后,它不起作用,输入后便不会消失。 – fourth

回答

0

<!doctype html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
 
</head> 
 

 
<body ng-app=""> 
 
    <form name=signUpForm novalidate ng-submit="signUp()"> 
 
    <label for="nameInput">Name:</label> 
 
    <input type="text" id="nameInput" name="nameInput" class="form-control" placeholder="your name" ng-required="true" autocomplete="name" autofocus ng-model="nameModel"> 
 
    <div ng-show="signUpForm.$submitted || signUpForm.nameInput.$touched"> 
 
     <p style="color:red" ng-show="signUpForm.nameInput.$error.required">This field is required</p> 
 
    </div> 
 
    <button type="submit">Submit</button> 
 
    </form> 
 

 
</body> 
 

 
</html>

您需要ng-app初始化程序。 希望这个例子能帮助你。

+0

你可以使用ng消息吗?我认为我的代码不仅是ng-app的问题。源代码使用了ng-if和ng-message,它的工作原理 – fourth

+0

嘿,我发现第一个'ng-message'应该是'ng-messages',大声笑 – fourth

+0

我很高兴你找到了解决办法。 –