简体   繁体   English

基本的AngularJS表单验证不起作用

[英]Basic AngularJS Form Validation is not working

I'm using basic example from W3School and trying to validate AngularJS form. 我正在使用W3School的基本示例,并尝试验证AngularJS表单。 But in any browser form is not getting validated. 但是,在任何浏览器中,表单都没有得到验证。 Any suggestions on the same? 有什么建议吗?

<form name="myForm" novalidate>

    <p>Username:<br>
        <input type="text" name="user" ng-model="user" required>
        <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
            <span ng-show="myForm.user.$error.required">Username is required.</span>
        </span>
    </p>

    <p>Email:<br>
        <input type="email" name="email" ng-model="email" required>
        <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
            <span ng-show="myForm.email.$error.required">Email is required.</span>
            <span ng-show="myForm.email.$error.email">Invalid email address.</span>
        </span>
    </p>

    <p>
        <input type="submit" ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||  myForm.email.$dirty && myForm.email.$invalid">
    </p>

</form>

You have to include ng-app and ng-controller to your form. 您必须在表单中包含ng-app和ng-controller。 also declare the app and controller in a separate js file. 还要在单独的js文件中声明应用和控制器。

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

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