Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3374e35

Browse files
btfordpetebacondarwin
authored andcommitted
docs(ngValue): add docs for ngValue directive
Closes #4267
1 parent 90ff8a9 commit 3374e35

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/ng/directive/input.js

+52-3
Original file line numberDiff line numberDiff line change
@@ -1330,17 +1330,66 @@ var ngListDirective = function() {
13301330

13311331

13321332
var CONSTANT_VALUE_REGEXP = /^(true|false|\d+)$/;
1333-
1333+
/**
1334+
* @ngdoc directive
1335+
* @name ng.directive:ngValue
1336+
*
1337+
* @description
1338+
* Binds the given expression to the value of `input[select]` or `input[radio]`, so
1339+
* that when the element is selected, the `ngModel` of that element is set to the
1340+
* bound value.
1341+
*
1342+
* `ngValue` is useful when dynamically generating lists of radio buttons using `ng-repeat`, as
1343+
* shown below.
1344+
*
1345+
* @element input
1346+
* @param {string=} ngValue angular expression, whose value will be bound to the `value` attribute
1347+
* of the `input` element
1348+
*
1349+
* @example
1350+
<doc:example>
1351+
<doc:source>
1352+
<script>
1353+
function Ctrl($scope) {
1354+
$scope.names = ['pizza', 'unicorns', 'robots'];
1355+
$scope.my = { favorite: 'unicorns' };
1356+
}
1357+
</script>
1358+
<form ng-controller="Ctrl">
1359+
<h2>Which is your favorite?</h2>
1360+
<label ng-repeat="name in names" for="{{name}}">
1361+
{{name}}
1362+
<input type="radio"
1363+
ng-model="my.favorite"
1364+
ng-value="name"
1365+
id="{{name}}"
1366+
name="favorite">
1367+
</label>
1368+
</span>
1369+
<div>You chose {{my.favorite}}</div>
1370+
</form>
1371+
</doc:source>
1372+
<doc:scenario>
1373+
it('should initialize to model', function() {
1374+
expect(binding('my.favorite')).toEqual('unicorns');
1375+
});
1376+
it('should bind the values to the inputs', function() {
1377+
input('my.favorite').select('pizza');
1378+
expect(binding('my.favorite')).toEqual('pizza');
1379+
});
1380+
</doc:scenario>
1381+
</doc:example>
1382+
*/
13341383
var ngValueDirective = function() {
13351384
return {
13361385
priority: 100,
13371386
compile: function(tpl, tplAttr) {
13381387
if (CONSTANT_VALUE_REGEXP.test(tplAttr.ngValue)) {
1339-
return function(scope, elm, attr) {
1388+
return function ngValueConstantLink(scope, elm, attr) {
13401389
attr.$set('value', scope.$eval(attr.ngValue));
13411390
};
13421391
} else {
1343-
return function(scope, elm, attr) {
1392+
return function ngValueLink(scope, elm, attr) {
13441393
scope.$watch(attr.ngValue, function valueWatchAction(value) {
13451394
attr.$set('value', value);
13461395
});

0 commit comments

Comments
 (0)