From cf17c6af475eace31cf52944afd8e10d3afcf6c0 Mon Sep 17 00:00:00 2001
From: Luis Ramón López
Date: Tue, 26 Feb 2013 00:00:47 +0100
Subject: feat($compile): add attribute binding support via ngAttr*
Sometimes is not desirable to use interpolation on attributes because
the user agent parses them before the interpolation takes place. I.e:
The snippet throws three browser errors, one for each attribute.
For some attributes, AngularJS fixes that behaviour introducing special
directives like ng-href or ng-src.
This commit is a more general solution that allows prefixing any
attribute with "ng-attr-", "ng:attr:" or "ng_attr_" so it will
be set only when the binding is done. The prefix is then removed.
Example usage:
Closes #1050
Closes #1925
---
src/ng/compile.js | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
(limited to 'src')
diff --git a/src/ng/compile.js b/src/ng/compile.js
index 48beb61e..3746bb66 100644
--- a/src/ng/compile.js
+++ b/src/ng/compile.js
@@ -349,7 +349,8 @@ function $CompileProvider($provide) {
? identity
: function denormalizeTemplate(template) {
return template.replace(/\{\{/g, startSymbol).replace(/}}/g, endSymbol);
- };
+ },
+ NG_ATTR_BINDING = /^ngAttr[A-Z]/;
return compile;
@@ -514,11 +515,16 @@ function $CompileProvider($provide) {
directiveNormalize(nodeName_(node).toLowerCase()), 'E', maxPriority);
// iterate over the attributes
- for (var attr, name, nName, value, nAttrs = node.attributes,
+ for (var attr, name, nName, ngAttrName, value, nAttrs = node.attributes,
j = 0, jj = nAttrs && nAttrs.length; j < jj; j++) {
attr = nAttrs[j];
if (attr.specified) {
name = attr.name;
+ // support ngAttr attribute binding
+ ngAttrName = directiveNormalize(name);
+ if (NG_ATTR_BINDING.test(ngAttrName)) {
+ name = ngAttrName.substr(6).toLowerCase();
+ }
nName = directiveNormalize(name.toLowerCase());
attrsMap[nName] = name;
attrs[nName] = value = trim((msie && name == 'href')
--
cgit v1.2.3