目录搜索
AngularJS API Referenceautoauto/serviceauto/service/$injectorauto/service/$providengng/directiveng/directive/ang/directive/formng/directive/inputng/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/ngAppng/directive/ngBindng/directive/ngBindHtmlng/directive/ngBindTemplateng/directive/ngBlurng/directive/ngChangeng/directive/ngCheckedng/directive/ngClassng/directive/ngClassEvenng/directive/ngClassOddng/directive/ngClickng/directive/ngCloakng/directive/ngControllerng/directive/ngCopyng/directive/ngCspng/directive/ngCutng/directive/ngDblclickng/directive/ngDisabledng/directive/ngFocusng/directive/ngFormng/directive/ngHideng/directive/ngHrefng/directive/ngIfng/directive/ngIncludeng/directive/ngInitng/directive/ngKeydownng/directive/ngKeypressng/directive/ngKeyupng/directive/ngListng/directive/ngModelng/directive/ngModelOptionsng/directive/ngMousedownng/directive/ngMouseenterng/directive/ngMouseleaveng/directive/ngMousemoveng/directive/ngMouseoverng/directive/ngMouseupng/directive/ngNonBindableng/directive/ngOpenng/directive/ngPasteng/directive/ngPluralizeng/directive/ngReadonlyng/directive/ngRepeatng/directive/ngSelectedng/directive/ngShowng/directive/ngSrcng/directive/ngSrcsetng/directive/ngStyleng/directive/ngSubmitng/directive/ngSwitchng/directive/ngTranscludeng/directive/ngValueng/directive/scriptng/directive/selectng/directive/textareang/filterng/filter/currencyng/filter/dateng/filter/filterng/filter/jsonng/filter/limitTong/filter/lowercaseng/filter/numberng/filter/orderByng/filter/uppercaseng/functionng/function/angular.bindng/function/angular.bootstrapng/function/angular.copyng/function/angular.elementng/function/angular.equalsng/function/angular.extendng/function/angular.forEachng/function/angular.fromJsonng/function/angular.identityng/function/angular.injectorng/function/angular.isArrayng/function/angular.isDateng/function/angular.isDefinedng/function/angular.isElementng/function/angular.isFunctionng/function/angular.isNumberng/function/angular.isObjectng/function/angular.isStringng/function/angular.isUndefinedng/function/angular.lowercaseng/function/angular.moduleng/function/angular.noopng/function/angular.toJsonng/function/angular.uppercaseng/objectng/object/angular.versionng/providerng/provider/$animateProviderng/provider/$compileProviderng/provider/$controllerProviderng/provider/$filterProviderng/provider/$httpProviderng/provider/$interpolateProviderng/provider/$locationProviderng/provider/$logProviderng/provider/$parseProviderng/provider/$rootScopeProviderng/provider/$sceDelegateProviderng/provider/$sceProviderng/serviceng/service/$anchorScrollng/service/$animateng/service/$cacheFactoryng/service/$compileng/service/$controllerng/service/$documentng/service/$exceptionHandlerng/service/$filterng/service/$httpng/service/$httpBackendng/service/$interpolateng/service/$intervalng/service/$localeng/service/$locationng/service/$logng/service/$parseng/service/$qng/service/$rootElementng/service/$rootScopeng/service/$sceng/service/$sceDelegateng/service/$templateCacheng/service/$timeoutng/service/$windowng/typeng/type/$cacheFactory.Cacheng/type/$compile.directive.Attributesng/type/$rootScope.Scopeng/type/angular.Moduleng/type/form.FormControllerng/type/ngModel.NgModelControllerngAnimatengAnimate/providerngAnimate/provider/$animateProviderngAnimate/servicengAnimate/service/$animatengCookiesngCookies/servicengCookies/service/$cookiesngCookies/service/$cookieStorengMessagesngMessages/directivengMessages/directive/ngMessagengMessages/directive/ngMessagesngMockngMock/functionngMock/function/angular.mock.dumpngMock/function/angular.mock.injectngMock/function/angular.mock.modulengMock/objectngMock/object/angular.mockngMock/providerngMock/provider/$exceptionHandlerProviderngMock/servicengMock/service/$exceptionHandlerngMock/service/$httpBackendngMock/service/$intervalngMock/service/$logngMock/service/$timeoutngMock/typengMock/type/angular.mock.TzDatengMockE2EngMockE2E/servicengMockE2E/service/$httpBackendngResourcengResource/servicengResource/service/$resourcengRoutengRoute/directivengRoute/directive/ngViewngRoute/providerngRoute/provider/$routeProviderngRoute/servicengRoute/service/$routengRoute/service/$routeParamsngSanitizengSanitize/filterngSanitize/filter/linkyngSanitize/servicengSanitize/service/$sanitizengTouchngTouch/directivengTouch/directive/ngClickngTouch/directive/ngSwipeLeftngTouch/directive/ngSwipeRightngTouch/servicengTouch/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');});
上一篇:下一篇: