简体   繁体   English

检查没有名称的ng-form的有效性

[英]Check validity of ng-form with no name

How can I check the validity of ng-form with no name? 如何检查没有名称的ng-form的有效性?

<div ng-form>
  <input required name="xxx" />
  <p>valid: {{$valid}}</p>
  <p>pristine: {{$pristine}}</p>
</div>

normally with a name I would do 通常用我会做的名字

<div ng-form="test">
  <input required name="xxx" />
  <p>valid: {{test.$valid}}</p>
  <p>pristine: {{test.$pristine}}</p>
</div>

but I have a directive that spits out multiple of these ng-forms on a page and their names are colliding in the scope. 但是我有一条指令,它会在页面上吐出多个ng形式,并且它们的名称在范围内冲突。 so i would like to not give them a name instead of trying to give them all unique names. 所以我不想给他们一个名字,而不是试图给他们所有唯一的名字。

Can you write a directive for your each of your forms? 您可以为每种表单编写指令吗? Without seeing more of your code, I wrote two directives - One for the form container itself and one for the inputs inside it: 在没有看到更多代码的情况下,我编写了两个指令-一个用于表单容器本身,另一个用于其中的输入:

var mod = angular.module("myApp", []);

mod.directive("myForm", function () {
    return {
        restrict: "A",
        transclude: true,
        template: '<div ng-form ng-transclude></div>',
        replace: true,
        scope: true,
        controller: function () {}
    }
});

mod.directive("myInputs", function () {
    return {
        restrict: "A",
        require: ["^form", "^myForm"],
        template: '<div><input ng-model="name" required name="xxx" /><p>valid: {{form.$valid}}</p><p>pristine: {{form.$pristine}}</p></div>',
        replace: true,
        link: function (scope, element, attrs, ctrls) {
            scope.form = ctrls[0];
        }
    }
});

The HTML looks as follows (reuse directives as many times as needed): HTML如下所示(根据需要重复使用指令多次):

<div ng-app="myApp">
    <div my-form>
        <div my-inputs></div>
    </div>
    <div my-form>
        <div my-inputs></div>
    </div>
    <div my-form>
        <div my-inputs></div>
    </div>
</div>

What I'm doing is using the container form (myForm directive) to create an ng-form with no name. 我正在做的是使用容器表单(myForm指令)创建不带名称的ng表单。 Then the "child" directive requires "form" and "myForm" (which it gets from the parent). 然后,“子”指令需要“窗体”和“ myForm”(从父级获取)。 Once it has this, it binds the form controller to the scope (which is prototypically inherited by the parent so there are no collisions). 一旦有了它,它将窗体控制器绑定到范围(原型通常由父级继承,因此不会发生冲突)。 I also had to give the input an ng-model so it registered itself with the form. 我还必须给输入一个ng模型,以便它使用表单注册自己。

See jsfiddle for demo: http://jsfiddle.net/Cv8zH/ 参见jsfiddle进行演示: http : //jsfiddle.net/Cv8zH/

Each my-form directive maintains $valid and $pristine with no need for name. 每个my-form指令都维护$ valid和$ pristine而不需要名称。

Update: If you need to have different inputs for each form, you can do something similar, just transclude the inputs: See updated fiddle: http://jsfiddle.net/Cv8zH/1/ 更新:如果每种表单需要不同的输入,则可以执行类似的操作,只需将输入包括在内即可:请参阅更新的提琴: http : //jsfiddle.net/Cv8zH/1/

<div ng-app="myApp">
    <div my-form>
        <div my-inputs>
            Name: * <input name="myName" ng-model="name" required><br>
            Phone: * <input name="myPhone" ng-model="phone" required ng-pattern="/\d{3}-\d{4}/">
              <p>valid: {{form.$valid}}</p>
              <p>pristine: {{form.$pristine}}</p>
        </div>
    </div>
    <div my-form>
        <div my-inputs>
            Hobby: <input name="hobby" ng-model="hobby">
              <p>valid: {{form.$valid}}</p>
              <p>pristine: {{form.$pristine}}</p>    
        </div>
    </div>
    <div my-form>
        <div my-inputs>
            Address: * <input name="address" ng-model="address" required>
              <p>valid: {{form.$valid}}</p>
              <p>pristine: {{form.$pristine}}</p>
        </div>
    </div>
</div>

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

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