Javascript Angular 表单验证 ng-disabled 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28568962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:01:04  来源:igfitidea点击:

Angular form validation ng-disabled not working

javascripthtmlangularjsformsvalidation

提问by Billy

I am trying to use the angular form validations in my form for a blog post, and more specifically a ng-disabled form. For reasons I cannot figure out the submit button is not disabled, where as it should be unless all three input fields are valid. Thanks for the help.

我正在尝试在我的表单中使用角度表单验证来撰写博客文章,更具体地说是禁用 ng 的表单。出于某些原因,我无法确定提交按钮没有被禁用,除非所有三个输入字段都有效,否则应该禁用。谢谢您的帮助。

this is my blog template

这是我的博客模板

 <div ng-controller='BlgoCtrl'>
  <div class='container'>
    <h1> Teewinot Blgo</h1>
    <div class="row">
      <div class='col-md-12'>
        <form role='form' name='blgoPost' novalidate>
          <div class='form-group'>
          <label for='blgoTitle'>Title</label>
          <input name='title' type="title" class='form-control'  placeholder='Technologies of the Future' required><br>
          <label for='blgoContent'>Content</label>
          <textarea name='content' rows='8' type="content" class='form-control' placeholder='The technical innovations of the future will be diverse and impactful on the individual......' required></textarea><br>
          <label for='blgoPassCode'>PassCode</label>
          <input name='passcode' type="passcode" class='form-control' placeholder='&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;&#8226;' required><br>
          <button type="submit" class='btn btn-primary' ng-disabled="blgoPost.$invalid">Submit Post</button>
          </div>
        </form>
      </div>
    </div>

Here is my index.html

这是我的 index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Teewinot</title>

  <script src="bower_components/angular/angular.js"></script>

  <script src="bower_components/jquery/dist/jquery.js"></script>
  <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="bower_components/angular-route/angular-route.js"></script>
  <link href="bower_components/bootstrap/dist/css/bootstrap.css" rel="stylesheet">

</head>

<body ng-app="Teewinot">
<ng-include src="'app/templates/partials/navbar.html'"></ng-include>

<ng-view></ng-view> 
<!-- angular module defintion and reoutes -->
<script src="app/js/app.js"></script>
<script src="app/js/routes.js"></script>
<!-- controllers -->
<script src="app/js/controllers/blog.controller.js"></script>

</body>
</html>

This is my blog controller

这是我的博客控制器

angular.module('Teewinot').controller('BlgoCtrl', function($scope, $http) {
  'use strict'
});

回答by Pankaj Parkar

You're missing ng-modelon every field of your form. Keep in mind when you mention ng-modelon any form field at that time ng-modelcreates the extra objects inside form name object with that specific nameattribute which then considered while form validation like $error, $valid, $invalid, etc.

您缺少ng-model表单的每个字段。请记住,当你提到ng-model在当时任何形式的字段ng-model创建表单名对象中的额外的对象与特定的name属性,然后考虑而表单验证一样$error$valid$invalid,等。

As your form nameis blgoPost, when angular compile this page,it internally creates the object inside the scope of the name of blgoPost. And the all the input fields which has name& ng-modelassign to them gets added inside that blgoPostobject. But if you don't mention ng-modelto the form fields then it will never get added inside the blgoPostform object.

当您的形式nameblgoPost,当角编译该页面时,它在内部创建的名称的范围内的对象blgoPost。并且所有具有name&ng-model分配给它们的输入字段都被添加到该blgoPost对象中。但是,如果您不提及ng-model表单字段,那么它将永远不会被添加到blgoPost表单对象中。

HTML

HTML

<form role='form' name='blgoPost' novalidate="">
    <input name="first" />
    <div class='form-group'>
        <label for='blgoTitle'>Title</label>
        <input name='title' type="title" class='form-control' ng-model="test1" placeholder='Technologies of the Future' required="">
        <br>
        <label for='blgoContent'>Content</label>
        <textarea name='content' rows='8' type="content" ng-model="test2" class='form-control' placeholder='The technical innovations of the future will be diverse and impactful on the individual......' required=""></textarea>
        <br>
        <label for='blgoPassCode'>PassCode</label>
        <input name='passcode' type="passcode" ng-model="test3" class='form-control' placeholder='&#8226;&#8226;&#8226' required="" />
        <br/>
        <button type="submit" class='btn btn-primary' ng-disabled="blgoPost.$invalid">Submit Post</button>
    </div>
</form>

Working Fiddle

工作小提琴