aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVojta Jina2012-03-23 13:04:52 -0700
committerVojta Jina2012-03-26 21:14:09 -0700
commit09e175f02cca0f4a295fd0c9b980cd8f432e722b (patch)
tree49796ba88d2db7a6e621155e9849109206f744cd /src
parent5c5b1183c82a28841b3e1e246ee341262e91d743 (diff)
downloadangular.js-09e175f02cca0f4a295fd0c9b980cd8f432e722b.tar.bz2
feat(ngValue): allow radio inputs to have non string values
Closes #816
Diffstat (limited to 'src')
-rw-r--r--src/AngularPublic.js3
-rw-r--r--src/directive/input.js26
-rw-r--r--src/service/compiler.js17
3 files changed, 38 insertions, 8 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js
index c922c5e2..65ebe3f0 100644
--- a/src/AngularPublic.js
+++ b/src/AngularPublic.js
@@ -101,7 +101,8 @@ function publishExternalAPI(angular){
ngChange: ngChangeDirective,
ngModelInstant: ngModelInstantDirective,
required: requiredDirective,
- ngRequired: requiredDirective
+ ngRequired: requiredDirective,
+ ngValue: ngValueDirective
}).
directive(ngAttributeAliasDirectives).
directive(ngEventDirectives);
diff --git a/src/directive/input.js b/src/directive/input.js
index c9553e39..348c9f25 100644
--- a/src/directive/input.js
+++ b/src/directive/input.js
@@ -558,7 +558,7 @@ function radioInputType(scope, element, attr, ctrl) {
ctrl.$render = function() {
var value = attr.value;
- element[0].checked = isDefined(value) && (value == ctrl.$viewValue);
+ element[0].checked = (value == ctrl.$viewValue);
};
attr.$observe('value', ctrl.$render);
@@ -1168,3 +1168,27 @@ var ngListDirective = function() {
}
};
};
+
+
+var CONSTANT_VALUE_REGEXP = /^(true|false|\d+)$/;
+
+var ngValueDirective = [function() {
+ return {
+ priority: 100,
+ compile: function(tpl, attr) {
+ if (CONSTANT_VALUE_REGEXP.test(attr.ngValue)) {
+ return function(scope) {
+ attr.$set('value', scope.$eval(attr.ngValue));
+ };
+ } else {
+ attr.$observers.value = [];
+
+ return function(scope) {
+ scope.$watch(attr.ngValue, function(value) {
+ attr.$set('value', value, false);
+ });
+ };
+ }
+ }
+ };
+}];
diff --git a/src/service/compiler.js b/src/service/compiler.js
index 9e03f186..8ddf77ae 100644
--- a/src/service/compiler.js
+++ b/src/service/compiler.js
@@ -732,7 +732,7 @@ function $CompileProvider($provide) {
if (src[key]) {
value += (key === 'style' ? ';' : ' ') + src[key];
}
- dst.$set(key, value, srcAttr[key]);
+ dst.$set(key, value, true, srcAttr[key]);
}
});
// copy the new attributes on the old attrs object
@@ -937,9 +937,11 @@ function $CompileProvider($provide) {
* can share the attribute. This function properly handles boolean attributes.
* @param {string} key Normalized key. (ie ngAttribute)
* @param {string|boolean} value The value to set. If `null` attribute will be deleted.
+ * @param {boolean=} writeAttr If false, does not write the value to DOM element attribute.
+ * Defaults to true.
* @param {string=} attrName Optional none normalized name. Defaults to key.
*/
- function attrSetter(key, value, attrName) {
+ function attrSetter(key, value, writeAttr, attrName) {
var booleanKey = isBooleanAttr(this.$element[0], key.toLowerCase());
if (booleanKey) {
@@ -962,12 +964,15 @@ function $CompileProvider($provide) {
}
}
- if (value === null || value === undefined) {
- this.$element.removeAttr(attrName);
- } else {
- this.$element.attr(attrName, value);
+ if (writeAttr !== false) {
+ if (value === null || value === undefined) {
+ this.$element.removeAttr(attrName);
+ } else {
+ this.$element.attr(attrName, value);
+ }
}
+
// fire observers
forEach(this.$observers[key], function(fn) {
try {