mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
parent
90ff8a98d8
commit
3374e35953
1 changed files with 52 additions and 3 deletions
|
|
@ -1330,17 +1330,66 @@ var ngListDirective = function() {
|
|||
|
||||
|
||||
var CONSTANT_VALUE_REGEXP = /^(true|false|\d+)$/;
|
||||
|
||||
/**
|
||||
* @ngdoc directive
|
||||
* @name ng.directive:ngValue
|
||||
*
|
||||
* @description
|
||||
* Binds the given expression to the value of `input[select]` or `input[radio]`, so
|
||||
* that when the element is selected, the `ngModel` of that element is set to the
|
||||
* bound value.
|
||||
*
|
||||
* `ngValue` is useful when dynamically generating lists of radio buttons using `ng-repeat`, as
|
||||
* shown below.
|
||||
*
|
||||
* @element input
|
||||
* @param {string=} ngValue angular expression, whose value will be bound to the `value` attribute
|
||||
* of the `input` element
|
||||
*
|
||||
* @example
|
||||
<doc:example>
|
||||
<doc:source>
|
||||
<script>
|
||||
function Ctrl($scope) {
|
||||
$scope.names = ['pizza', 'unicorns', 'robots'];
|
||||
$scope.my = { favorite: 'unicorns' };
|
||||
}
|
||||
</script>
|
||||
<form ng-controller="Ctrl">
|
||||
<h2>Which is your favorite?</h2>
|
||||
<label ng-repeat="name in names" for="{{name}}">
|
||||
{{name}}
|
||||
<input type="radio"
|
||||
ng-model="my.favorite"
|
||||
ng-value="name"
|
||||
id="{{name}}"
|
||||
name="favorite">
|
||||
</label>
|
||||
</span>
|
||||
<div>You chose {{my.favorite}}</div>
|
||||
</form>
|
||||
</doc:source>
|
||||
<doc:scenario>
|
||||
it('should initialize to model', function() {
|
||||
expect(binding('my.favorite')).toEqual('unicorns');
|
||||
});
|
||||
it('should bind the values to the inputs', function() {
|
||||
input('my.favorite').select('pizza');
|
||||
expect(binding('my.favorite')).toEqual('pizza');
|
||||
});
|
||||
</doc:scenario>
|
||||
</doc:example>
|
||||
*/
|
||||
var ngValueDirective = function() {
|
||||
return {
|
||||
priority: 100,
|
||||
compile: function(tpl, tplAttr) {
|
||||
if (CONSTANT_VALUE_REGEXP.test(tplAttr.ngValue)) {
|
||||
return function(scope, elm, attr) {
|
||||
return function ngValueConstantLink(scope, elm, attr) {
|
||||
attr.$set('value', scope.$eval(attr.ngValue));
|
||||
};
|
||||
} else {
|
||||
return function(scope, elm, attr) {
|
||||
return function ngValueLink(scope, elm, attr) {
|
||||
scope.$watch(attr.ngValue, function valueWatchAction(value) {
|
||||
attr.$set('value', value);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue