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

Commit afc1fe4

Browse files
committed
feat($compile): support expansion of special ngAutobind attributes
In order to reduce some of the verbosity associated with conforming to the ideas behind unidirectional data-flow, we can introduce a special attribute syntax that will be automatically expanded in order to simulate two-way data binding. For example, `<my-component ng-autobind-foo="bar">` will be expanded into `<my-component foo="bar" foo-changed="bar = $event">` Fixes #15455
1 parent f5d2bf3 commit afc1fe4

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/ng/compile.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18031803
: function denormalizeTemplate(template) {
18041804
return template.replace(/\{\{/g, startSymbol).replace(/}}/g, endSymbol);
18051805
},
1806-
NG_ATTR_BINDING = /^ngAttr[A-Z]/;
1806+
NG_ATTR_BINDING = /^ng(Attr|Autobind)[A-Z]/;
18071807
var MULTI_ELEMENT_DIR_RE = /^(.+)Start$/;
18081808

18091809
compile.$$addBindingInfo = debugInfoEnabled ? function $$addBindingInfo($element, binding) {
@@ -2150,10 +2150,14 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21502150

21512151
// support ngAttr attribute binding
21522152
ngAttrName = directiveNormalize(name);
2153-
isNgAttr = NG_ATTR_BINDING.test(ngAttrName);
2154-
if (isNgAttr) {
2153+
2154+
var isSpecialAttr = NG_ATTR_BINDING.exec(ngAttrName);
2155+
var isNgAttr = isSpecialAttr && isSpecialAttr[1] === 'Attr';
2156+
var isNgAutobind = isSpecialAttr && isSpecialAttr[1] === 'Autobind';
2157+
2158+
if (isSpecialAttr) {
21552159
name = name.replace(PREFIX_REGEXP, '')
2156-
.substr(8).replace(/_(.)/g, function(match, letter) {
2160+
.substr(isNgAttr ? 8 : 12).replace(/_(.)/g, function(match, letter) {
21572161
return letter.toUpperCase();
21582162
});
21592163
}
@@ -2173,6 +2177,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21732177
attrs[nName] = true; // presence means true
21742178
}
21752179
}
2180+
2181+
if (isNgAutobind) {
2182+
attrs[name] = value;
2183+
attrs[name + 'Change'] = value + '= $event';
2184+
}
2185+
21762186
addAttrInterpolateDirective(node, directives, value, nName, isNgAttr);
21772187
addDirective(directives, nName, 'A', maxPriority, ignoreDirective, attrStartName,
21782188
attrEndName);

0 commit comments

Comments
 (0)