예배 규칙서 찾다
AngularJS API Reference auto auto/service auto/service/$injector auto/service/$provide ng ng/directive ng/directive/a ng/directive/form ng/directive/input ng/directive/input[checkbox] ng/directive/input[date] ng/directive/input[dateTimeLocal] ng/directive/input[email] ng/directive/input[month] ng/directive/input[number] ng/directive/input[radio] ng/directive/input[text] ng/directive/input[time] ng/directive/input[url] ng/directive/input[week] ng/directive/ngApp ng/directive/ngBind ng/directive/ngBindHtml ng/directive/ngBindTemplate ng/directive/ngBlur ng/directive/ngChange ng/directive/ngChecked ng/directive/ngClass ng/directive/ngClassEven ng/directive/ngClassOdd ng/directive/ngClick ng/directive/ngCloak ng/directive/ngController ng/directive/ngCopy ng/directive/ngCsp ng/directive/ngCut ng/directive/ngDblclick ng/directive/ngDisabled ng/directive/ngFocus ng/directive/ngForm ng/directive/ngHide ng/directive/ngHref ng/directive/ngIf ng/directive/ngInclude ng/directive/ngInit ng/directive/ngKeydown ng/directive/ngKeypress ng/directive/ngKeyup ng/directive/ngList ng/directive/ngModel ng/directive/ngModelOptions ng/directive/ngMousedown ng/directive/ngMouseenter ng/directive/ngMouseleave ng/directive/ngMousemove ng/directive/ngMouseover ng/directive/ngMouseup ng/directive/ngNonBindable ng/directive/ngOpen ng/directive/ngPaste ng/directive/ngPluralize ng/directive/ngReadonly ng/directive/ngRepeat ng/directive/ngSelected ng/directive/ngShow ng/directive/ngSrc ng/directive/ngSrcset ng/directive/ngStyle ng/directive/ngSubmit ng/directive/ngSwitch ng/directive/ngTransclude ng/directive/ngValue ng/directive/script ng/directive/select ng/directive/textarea ng/filter ng/filter/currency ng/filter/date ng/filter/filter ng/filter/json ng/filter/limitTo ng/filter/lowercase ng/filter/number ng/filter/orderBy ng/filter/uppercase ng/function ng/function/angular.bind ng/function/angular.bootstrap ng/function/angular.copy ng/function/angular.element ng/function/angular.equals ng/function/angular.extend ng/function/angular.forEach ng/function/angular.fromJson ng/function/angular.identity ng/function/angular.injector ng/function/angular.isArray ng/function/angular.isDate ng/function/angular.isDefined ng/function/angular.isElement ng/function/angular.isFunction ng/function/angular.isNumber ng/function/angular.isObject ng/function/angular.isString ng/function/angular.isUndefined ng/function/angular.lowercase ng/function/angular.module ng/function/angular.noop ng/function/angular.toJson ng/function/angular.uppercase ng/object ng/object/angular.version ng/provider ng/provider/$animateProvider ng/provider/$compileProvider ng/provider/$controllerProvider ng/provider/$filterProvider ng/provider/$httpProvider ng/provider/$interpolateProvider ng/provider/$locationProvider ng/provider/$logProvider ng/provider/$parseProvider ng/provider/$rootScopeProvider ng/provider/$sceDelegateProvider ng/provider/$sceProvider ng/service ng/service/$anchorScroll ng/service/$animate ng/service/$cacheFactory ng/service/$compile ng/service/$controller ng/service/$document ng/service/$exceptionHandler ng/service/$filter ng/service/$http ng/service/$httpBackend ng/service/$interpolate ng/service/$interval ng/service/$locale ng/service/$location ng/service/$log ng/service/$parse ng/service/$q ng/service/$rootElement ng/service/$rootScope ng/service/$sce ng/service/$sceDelegate ng/service/$templateCache ng/service/$timeout ng/service/$window ng/type ng/type/$cacheFactory.Cache ng/type/$compile.directive.Attributes ng/type/$rootScope.Scope ng/type/angular.Module ng/type/form.FormController ng/type/ngModel.NgModelController ngAnimate ngAnimate/provider ngAnimate/provider/$animateProvider ngAnimate/service ngAnimate/service/$animate ngCookies ngCookies/service ngCookies/service/$cookies ngCookies/service/$cookieStore ngMessages ngMessages/directive ngMessages/directive/ngMessage ngMessages/directive/ngMessages ngMock ngMock/function ngMock/function/angular.mock.dump ngMock/function/angular.mock.inject ngMock/function/angular.mock.module ngMock/object ngMock/object/angular.mock ngMock/provider ngMock/provider/$exceptionHandlerProvider ngMock/service ngMock/service/$exceptionHandler ngMock/service/$httpBackend ngMock/service/$interval ngMock/service/$log ngMock/service/$timeout ngMock/type ngMock/type/angular.mock.TzDate ngMockE2E ngMockE2E/service ngMockE2E/service/$httpBackend ngResource ngResource/service ngResource/service/$resource ngRoute ngRoute/directive ngRoute/directive/ngView ngRoute/provider ngRoute/provider/$routeProvider ngRoute/service ngRoute/service/$route ngRoute/service/$routeParams ngSanitize ngSanitize/filter ngSanitize/filter/linky ngSanitize/service ngSanitize/service/$sanitize ngTouch ngTouch/directive ngTouch/directive/ngClick ngTouch/directive/ngSwipeLeft ngTouch/directive/ngSwipeRight ngTouch/service ngTouch/service/$swipe
문자

AngularJS: API: ng/directive/form

form

  1. - directive in module ng

指令会实例化 FormController

如果声明了 name 属性,表单控制器会被发布到name对应的作用域下。

别名: ngForm

Angular中的表单能够嵌套。 意思是当所有子表单有效时外部表单也会有效。然而,浏览器不允许 <form> 元素嵌套,所以Angular提供了 ngForm 指令来实现 <form> 相同的行为,并能进行嵌套。嵌套表单让你在使用 ngRepeat 指令动态生成的表单上使用Angula的校验指令时更方便。在此以前,由于使用修改方式不能动态生成输入元素的 name 属性,你必须使用 ngForm 指令包裹重复生成的集合中的每个输入对象,再在外部嵌套 form元素。

CSS类

  • ng-valid 表单通过验证时设置
  • ng-invalid 表单未通过验证时设置
  • ng-pristine 表单没有改动时设置
  • ng-dirty 表单有改动时设置

记住 ngAnimate 能侦测这些类中每个的添加和删除。

提交一个表单并防止默认动作

在富客户端式Angular应用中的表单身份不同于在以往的循环链式应用中的身份。为防止浏览器在发送数据到服务器时不会由于表单提交引起整个页面重载,所以在应用中通过特殊手段触发javascript逻辑来处理表单提交。

为此,Angular防止默认操作(表单提交到服务器),除非 <form> 元素指定了 action 属性.

你可以使用以下两种方法之一来指定表单提交时要调用的javascript方法:

  • form元素上的ngSubmit 指令
  • 第一个按钮或类型为submit的input字段(input[type=submit])上的ngClick 指令

为了防止处理程序的双重执行,你只能使用 ngSubmit 或 ngClick 指令的其中一个。这是因为HTML规范有以下表单提交规则:

  • 如果表单只有一个input字段,在这个字段按回车将触发表单提交(ngSubmit)
  • 如果表单有两个以上input字段并且没有按钮或没有input[type=submit] ,按回车不会触发提交
  • 如果表单有一个或多个input字段,并且有一个或多个按钮,或有input[type=submit],在任意input字段中按回车后将触发 第一个 按钮或 input[type=submit]上的click事件 (ngClick) 整个表单的submit事件 (ngSubmit)

当整个表单提交时,任何挂起的 ngModelOptions 更改将立即生效。注意, ngClick 事件将在模型被更新前触发。可以使用 ngSubmit访问更新的模型。

指令信息

  • 这个指令执行优先级为0.

用法

  • 作为元素使用:
    <form
      [name=""]>
    ...
    </form>

参数

参数 类型 详述
name
(可选)
string

表单名称。如果指定,表单控制器将被发布到这个名字下相关的域范围中。

动画钩子

ngForm中的动画将在任何级联CSS类被添加或删除时触发。这此类有: .ng-pristine, .ng-dirty, .ng-invalid.ng-valid ,以及表单执行的任何其他验证。ngForm中的动画类似ngClass中的工作方式,并且动画可以挂在 CSS 过渡、关键帧、JS动画上。

下面的例子简单的演示了一个利用 CSS 样式过渡来渲染一个验证时未通过验证的表单元素:

//be sure to include ngAnimate as a module to hook into more
//advanced animations
.my-form {
  transition:0.5s linear all;
  background: white;
}
.my-form.ng-invalid {
  background: red;
  color:white;
}

示例

index.html
<script>
  angular.module('formExample', [])
    .controller('FormController', ['$scope', Function($scope) {
      $scope.userType = 'guest';
    }]);
</script>
<style>
 .my-form {
   -webkit-transition:all linear 0.5s;
   transition:all linear 0.5s;
   background: transparent;
 }
 .my-form.ng-invalid {
   background: red;
 }
</style>
<form name="myForm" ng-controller="FormController" class="my-form">
  userType: <input name="input" ng-model="userType" required>
  <span class="error" ng-show="myForm.input.$error.required">Required!</span><br>
  <tt>userType = {{userType}}</tt><br>
  <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br>
  <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br>
  <tt>myForm.$valid = {{myForm.$valid}}</tt><br>
  <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br>
 </form>
protractor.js
it('should initialize to model', Function() {
  var userType = element(by.binding('userType'));
  var valid = element(by.binding('myForm.input.$valid'));

  expect(userType.getText()).toContain('guest');
  expect(valid.getText()).toContain('true');});

it('should be invalid if empty', Function() {
  var userType = element(by.binding('userType'));
  var valid = element(by.binding('myForm.input.$valid'));
  var userInput = element(by.model('userType'));

  userInput.clear();
  userInput.sendKeys('');

  expect(userType.getText()).toEqual('userType =');
  expect(valid.getText()).toContain('false');});
이전 기사: 다음 기사: