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

Commit 805efb4

Browse files
btfordpetebacondarwin
authored andcommitted
docs(ngValue): add docs for ngValue directive
Closes #4267
1 parent 28fe446 commit 805efb4

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
@@ -1367,17 +1367,66 @@ var ngListDirective = function() {
13671367

13681368

13691369
var CONSTANT_VALUE_REGEXP = /^(true|false|\d+)$/;
1370-
1370+
/**
1371+
* @ngdoc directive
1372+
* @name ng.directive:ngValue
1373+
*
1374+
* @description
1375+
* Binds the given expression to the value of `input[select]` or `input[radio]`, so
1376+
* that when the element is selected, the `ngModel` of that element is set to the
1377+
* bound value.
1378+
*
1379+
* `ngValue` is useful when dynamically generating lists of radio buttons using `ng-repeat`, as
1380+
* shown below.
1381+
*
1382+
* @element input
1383+
* @param {string=} ngValue angular expression, whose value will be bound to the `value` attribute
1384+
* of the `input` element
1385+
*
1386+
* @example
1387+
<doc:example>
1388+
<doc:source>
1389+
<script>
1390+
function Ctrl($scope) {
1391+
$scope.names = ['pizza', 'unicorns', 'robots'];
1392+
$scope.my = { favorite: 'unicorns' };
1393+
}
1394+
</script>
1395+
<form ng-controller="Ctrl">
1396+
<h2>Which is your favorite?</h2>
1397+
<label ng-repeat="name in names" for="{{name}}">
1398+
{{name}}
1399+
<input type="radio"
1400+
ng-model="my.favorite"
1401+
ng-value="name"
1402+
id="{{name}}"
1403+
name="favorite">
1404+
</label>
1405+
</span>
1406+
<div>You chose {{my.favorite}}</div>
1407+
</form>
1408+
</doc:source>
1409+
<doc:scenario>
1410+
it('should initialize to model', function() {
1411+
expect(binding('my.favorite')).toEqual('unicorns');
1412+
});
1413+
it('should bind the values to the inputs', function() {
1414+
input('my.favorite').select('pizza');
1415+
expect(binding('my.favorite')).toEqual('pizza');
1416+
});
1417+
</doc:scenario>
1418+
</doc:example>
1419+
*/
13711420
var ngValueDirective = function() {
13721421
return {
13731422
priority: 100,
13741423
compile: function(tpl, tplAttr) {
13751424
if (CONSTANT_VALUE_REGEXP.test(tplAttr.ngValue)) {
1376-
return function(scope, elm, attr) {
1425+
return function ngValueConstantLink(scope, elm, attr) {
13771426
attr.$set('value', scope.$eval(attr.ngValue));
13781427
};
13791428
} else {
1380-
return function(scope, elm, attr) {
1429+
return function ngValueLink(scope, elm, attr) {
13811430
scope.$watch(attr.ngValue, function valueWatchAction(value) {
13821431
attr.$set('value', value);
13831432
});

0 commit comments

Comments
 (0)