From 52535c3bf704a9c46aba1c92021987012cfb776a Mon Sep 17 00:00:00 2001 From: Gonzalo Ruiz de Villa Date: Fri, 21 Nov 2014 12:43:00 -0800 Subject: [PATCH] feat(form): add support for ngFormTopLevel attribute Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top level', therefore preventing the propagation of its state to its parent. I It maybe used like this: Closes: #5858 --- src/ng/directive/form.js | 21 +++- test/ng/directive/formSpec.js | 177 ++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index d88b02c5508f..3c3fe3fdd07c 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -65,6 +65,8 @@ function FormController(element, attrs, $scope, $animate, $interpolate) { var form = this, controls = []; + var topLevel = $scope.$eval(attrs.ngFormTopLevel) || false; + // init state form.$error = {}; form.$$success = {}; @@ -76,6 +78,7 @@ function FormController(element, attrs, $scope, $animate, $interpolate) { form.$invalid = false; form.$submitted = false; form.$$parentForm = nullFormCtrl; + form.$$topLevel = topLevel; /** * @ngdoc method @@ -318,6 +321,9 @@ function FormController(element, attrs, $scope, $animate, $interpolate) { * * @param {string=} ngForm|name Name of the form. If specified, the form controller will be published into * related scope, under this name. + * @param {boolean} ngFormTopLevel Value which indicates that the form should be considered as a top level + * and that it should not propagate its state to its parent form (if there is one). By default, + * child forms propagate their state ($dirty, $pristine, $valid, ...) to its parent form. * */ @@ -416,6 +422,10 @@ function FormController(element, attrs, $scope, $animate, $interpolate) { angular.module('formExample', []) .controller('FormController', ['$scope', function($scope) { $scope.userType = 'guest'; + $scope.submitted = false; + $scope.submit = function (){ + $scope.submitted = true; + } }]);