Angular 表单验证 ng-disabled 不起作用
Posted
技术标签:
【中文标题】Angular 表单验证 ng-disabled 不起作用【英文标题】:Angular form validation ng-disabled not working 【发布时间】:2015-04-18 14:08:34 【问题描述】:我正在尝试在我的表单中使用角度表单验证来撰写博客文章,更具体地说是禁用 ng 的表单。由于某些原因,我无法弄清楚提交按钮没有被禁用,除非所有三个输入字段都有效,否则应该是这样。谢谢您的帮助。
这是我的博客模板
<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='•••••••••••••••' required><br>
<button type="submit" class='btn btn-primary' ng-disabled="blgoPost.$invalid">Submit Post</button>
</div>
</form>
</div>
</div>
这是我的 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>
这是我的博客控制器
angular.module('Teewinot').controller('BlgoCtrl', function($scope, $http)
'use strict'
);
【问题讨论】:
设置 $invalid 值的代码在哪里? 将模块实例化为angular.module('Teewinot', [])
。将一个空数组传递给模块函数。并且,每个输入也要使用ng-model
。
【参考方案1】:
您在表单的每个字段中都缺少ng-model
。请记住,当您当时在任何表单字段上提及 ng-model
时,ng-model
在表单名称对象中创建了具有特定 name
属性的额外对象,然后在表单验证时考虑,例如 $error
、$valid
、@ 987654328@等
由于您的表单name
是blgoPost
,当角度编译此页面时,它会在内部创建blgoPost
名称范围内的对象。所有分配给它们的name
和ng-model
的输入字段都被添加到blgoPost
对象中。但是,如果您没有在表单字段中提及 ng-model
,那么它将永远不会被添加到 blgoPost
表单对象中。
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='•••' required="" />
<br/>
<button type="submit" class='btn btn-primary' ng-disabled="blgoPost.$invalid">Submit Post</button>
</div>
</form>
Working Fiddle
【讨论】:
以上是关于Angular 表单验证 ng-disabled 不起作用的主要内容,如果未能解决你的问题,请参考以下文章