mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat(ngForm): Supports expression in form names
<form name="ctrl.form"> form controller will accessible
as $scope.ctrl.form instead of $scope['ctrl.form']
BREAKING CHANGE:
If you have form names that will evaluate as an expression:
<form name="ctrl.form">
And if you are accessing the form from your controller:
Before:
function($scope) {
$scope['ctrl.form'] // form controller instance
}
After:
function($scope) {
$scope.ctrl.form // form controller instance
}
This makes it possible to access a form from a controller
using the new "controller as" syntax. Supporting the previous
behavior offers no benefit.
This commit is contained in:
parent
ad76e77fce
commit
4407e81c61
2 changed files with 29 additions and 4 deletions
|
|
@ -306,13 +306,13 @@ var formDirectiveFactory = function(isNgForm) {
|
|||
alias = attr.name || attr.ngForm;
|
||||
|
||||
if (alias) {
|
||||
scope[alias] = controller;
|
||||
setter(scope, alias, controller, alias);
|
||||
}
|
||||
if (parentFormCtrl) {
|
||||
formElement.bind('$destroy', function() {
|
||||
parentFormCtrl.$removeControl(controller);
|
||||
if (alias) {
|
||||
scope[alias] = undefined;
|
||||
setter(scope, alias, undefined, alias);
|
||||
}
|
||||
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
|
||||
});
|
||||
|
|
|
|||
|
|
@ -83,10 +83,11 @@ describe('form', function() {
|
|||
});
|
||||
|
||||
|
||||
it('should allow form name to be an expression', function() {
|
||||
it('should support expression in form name', function() {
|
||||
doc = $compile('<form name="obj.myForm"></form>')(scope);
|
||||
|
||||
expect(scope['obj.myForm']).toBeTruthy();
|
||||
expect(scope.obj).toBeDefined();
|
||||
expect(scope.obj.myForm).toBeTruthy();
|
||||
});
|
||||
|
||||
|
||||
|
|
@ -325,6 +326,30 @@ describe('form', function() {
|
|||
});
|
||||
|
||||
|
||||
it('should deregister a child form whose name is an expression when its DOM is removed', function() {
|
||||
doc = jqLite(
|
||||
'<form name="parent">' +
|
||||
'<div class="ng-form" name="child.form">' +
|
||||
'<input ng:model="modelA" name="inputA" required>' +
|
||||
'</div>' +
|
||||
'</form>');
|
||||
$compile(doc)(scope);
|
||||
scope.$apply();
|
||||
|
||||
var parent = scope.parent,
|
||||
child = scope.child.form;
|
||||
|
||||
expect(parent).toBeDefined();
|
||||
expect(child).toBeDefined();
|
||||
expect(parent.$error.required).toEqual([child]);
|
||||
doc.children().remove(); //remove child
|
||||
|
||||
expect(parent.child).toBeUndefined();
|
||||
expect(scope.child.form).toBeUndefined();
|
||||
expect(parent.$error.required).toBe(false);
|
||||
});
|
||||
|
||||
|
||||
it('should deregister a input when its removed from DOM', function() {
|
||||
doc = jqLite(
|
||||
'<form name="parent">' +
|
||||
|
|
|
|||
Loading…
Reference in a new issue