AngularJS <input>验证,不包含<form>


99

在Angular中是否可以通过验证<input>表单的类似方式来验证单个隔离对象?我正在考虑这样的事情:

<div class="form-group">
    <input name="myInput" type="text" class="form-control" ng-model="bindTo" ng-maxlength="5">
    <span class="error" ng-show="myInput.$error.maxlength">Too long!</span>
</div>

上面的示例不起作用。将其包含在中<form>并替换ng-showng-show="myForm.myInput.$error.maxlength"帮助。

不使用就可以做到这一点<form>吗?


2
你有试过吗 我不认为是这样,我相信Angular form.FormController在幕后创建了一个跟踪表单输入状态的幕后工具,例如valid\invalid & dirty\pristine. docs.angularjs.org/api/ng/type/form.FormController
meconroy

Answers:


184

您可以使用ng-form angular指令(请参阅此处的文档)对所有内容进行分组,即使是在html表单之外。然后,您可以利用有角度的FormController。

<div class="form-group" ng-form name="myForm">
    <input name="myInput" type="text" class="form-control" ng-model="bindTo" ng-maxlength="5">
    <span class="error" ng-show="myForm.myInput.$error.maxlength">Too long!</span>
</div>


接受了这个答案。那就是我一直在寻找的东西,尽管答案来得太晚了:)
Wojtek 2014年

1
这也帮助了我。我正在拔头发,偶然发现了这个。谢谢!
Alex McCabe 2015年

1
对于将来的读者,如果他们也想在按钮的ng-click事件上验证这种形式,请参见:stackoverflow.com/a/24123379/1371408
Matty J

具有单个验证的多个输入,例如plnkr.co/edit/wuOExkq4LXEiDELm2C6E?p=preview
Nathan Redblur

@SilvioLucas-即使该字段为空,您的示例仍然“执行” ...?
colmde

0

如果您要循环访问并且需要能够内插表单名称和有效状态,请以Silvio Lucas的答案为基础:

<div
  name="{{propertyName}}"
  ng-form=""
  class="property-edit-view"
  ng-class="{
    'has-error': {{propertyName}}.editBox.$invalid,
    'has-success':
      {{propertyName}}.editBox.$valid &&
      {{propertyName}}.editBox.$dirty &&
      propertyValue.length !== 0
  }"
  ng-switch="schema.type">
  <input
    name="editBox"
    ng-switch-when="int"
    type="number"
    ng-model="propertyValue"
    ng-pattern="/^[0-9]+$/"
    class="form-control">
  <input
    name="editBox"
    ng-switch-default=""
    type="text"
    ng-model="propertyValue"
    class="form-control">
  <span class="property-type" ng-bind="schema.type"></span>
</div>

-4
<!DOCTYPE html>
<html ng-app="plunker">
<head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js">   </script>

</head>

<body ng-controller="MainCtrl">
    <div class="help-block error" ng-show="test.field.$error.required">Required</div>
    <div class="help-block error" ng-show="test.firstName.$error.required">Name Required</div>
    <p>Hello {{name}}!</p>
    <div ng-form="test" id="test">
        <input type="text" name="firstName" ng-model="firstName" required> First name <br/> 
        <input id="field" name="field" required ng-model="field2" type="text"/>
    </div>
</body>
<script>
    var app = angular.module('plunker', []);

    app.controller('MainCtrl', function($scope) {
      $scope.name = 'World';
      $scope.field = "name";
      $scope.firstName = "FirstName";
      $scope.execute = function() {
        alert('Executed!');
      }
    });

</script>


1
这与stackoverflow.com/a/25342654/2333214有什么不同吗?如果是这样,您是否可以添加解释它的不同之处?
TJ

和解释?
Maximiliano Becerra
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.