This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(directive): ngName #6569
Closed
Closed
feat(directive): ngName #6569
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2510,3 +2510,113 @@ var ngModelOptionsDirective = function() { | |
}] | ||
}; | ||
}; | ||
|
||
/** | ||
* @ngdoc directive | ||
* @name ngName | ||
* | ||
* @priority 100 | ||
* | ||
* @description | ||
* The `ng-name` directive allows you the ability to evaluate scope expressions on the name attribute. | ||
* This directive does not react to the scope expression. It merely evaluates the expression, and sets the | ||
* {@link ngModel.NgModelController NgModelController}'s `$name` property and the `<input>` element's | ||
* `name` attribute. | ||
* | ||
* <div class="alert alert-info"> | ||
* This is particularly useful when building forms while looping through data with `ng-repeat`, | ||
* allowing you evaluate expressions such as as control names. | ||
* </div> | ||
* | ||
* <div class="alert alert-warning"> | ||
* This is <strong>NOT</strong> a data binding, in the sense that the attribute is not observed nor is the scope | ||
* expression watched. The ngName directive's link function runs after the ngModelController but before ngModel's | ||
* link function. This allows the evaluated result to be updated to the $name property prior to the | ||
* {@link form.FormController FormController}'s `$addControl` function being called. | ||
* </div> | ||
* | ||
* @element input | ||
* @param {expression} ngName {@link guide/expression Expression} to evaluate. | ||
* | ||
* @example | ||
<example name="ngName-directive"> | ||
<file name="index.html"> | ||
<div ng-controller="Ctrl"> | ||
<form name="candyForm"> | ||
<h2>Enter the amount of candy you want.</h2> | ||
<div ng-repeat="c in candy"> | ||
<label for="{{ c.type }}">{{ c.type }}</label> | ||
<input type="text" | ||
ng-model="c.qty" | ||
ng-name="c.type + 'Qty'" | ||
id="{{ c.type }}" | ||
ng-pattern="/^([1-9][0-9]*|0)$/" | ||
required> | ||
</div> | ||
</form> | ||
<br> | ||
<div ng-repeat="c in candy"> | ||
<div><b>{{ c.type }}</b></div> | ||
<div>candyForm.{{ c.type + 'Qty' }}.$valid = <b ng-bind="candyForm.{{ c.type + 'Qty' }}.$valid"></b></div> | ||
<div>Quantity = <b>{{ c.qty }}</b></div> | ||
<br> | ||
</div> | ||
</div> | ||
</file> | ||
<file name="script.js"> | ||
function Ctrl($scope) { | ||
$scope.candy = [ | ||
{ | ||
type: 'chocolates', | ||
qty: null | ||
}, | ||
{ | ||
type: 'peppermints', | ||
qty: null | ||
}, | ||
{ | ||
type: 'lollipops', | ||
qty: null | ||
} | ||
]; | ||
} | ||
</file> | ||
<file name="protractor.js" type="protractor"> | ||
var chocolatesInput = element(by.id('chocolates')); | ||
var chocolatesValid = element(by.binding('candyForm.chocolatesQty.$valid')); | ||
var peppermintsInput = element(by.id('peppermints')); | ||
var peppermintsValid = element(by.binding('candyForm.peppermintsQty.$valid')); | ||
var lollipopsInput = element(by.id('lollipops')); | ||
var lollipopsValid = element(by.binding('candyForm.lollipopsQty.$valid')); | ||
|
||
it('should initialize controls properly', function() { | ||
expect(chocolatesValid.getText()).toBe('false'); | ||
expect(peppermintsValid.getText()).toBe('false'); | ||
expect(lollipopsValid.getText()).toBe('false'); | ||
}); | ||
|
||
it('should be valid when entering n >= 0', function() { | ||
chocolatesInput.sendKeys('5'); | ||
peppermintsInput.sendKeys('55'); | ||
lollipopsInput.sendKeys('555'); | ||
|
||
expect(chocolatesValid.getText()).toBe('true'); | ||
expect(peppermintsValid.getText()).toBe('true'); | ||
expect(lollipopsValid.getText()).toBe('true'); | ||
}); | ||
</file> | ||
</example> | ||
*/ | ||
var ngNameDirective = function() { | ||
return { | ||
priority: 100, | ||
restrict: 'A', | ||
require: 'ngModel', | ||
link: { | ||
pre: function ngNameLinkFn(scope, elem, attrs, ctrl) { | ||
ctrl.$name = scope.$eval(attrs.ngName); | ||
attrs.$set('name', ctrl.$name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <carried from outdated diff> @petebacondarwin said: I don't believe this will work in certain versions of IE @sjbarker said: @petebacondarwin Wouldn't the Selenium Tests <catch> that? What versions do you think are affected? |
||
} | ||
} | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe this will work in certain versions of IE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@petebacondarwin Wouldn't the Selenium Tests caught that? What versions do you think are affected?