Skip to content

Commit b89c218

Browse files
jbedardNarretz
authored andcommitted
fix($compile): move check for interpolation of on-event attributes to compile time
This makes the two interpolation errors consistent and avoids checking the same thing per link (which previously would log the same error per link). The test changes are not necessary but do make them stricter and more like the selmulti error tests. Closes angular#13267 BREAKING CHANGE: Using interpolation in any on* event attributes (e.g. `<button onclick="{{myVar}}">`) will now throw the "nodomevents" error at compile time. Previously the nodomevents was thrown at link time. The new behavior makes it consistent with the "selmulti" error. The breaking change should be rare, as it relates to incorrect API use that should not make it to production apps in the first place.
1 parent 6b57198 commit b89c218

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/ng/compile.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -2956,19 +2956,19 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
29562956
startingTag(node));
29572957
}
29582958

2959+
if (EVENT_HANDLER_ATTR_REGEXP.test(name)) {
2960+
throw $compileMinErr('nodomevents',
2961+
"Interpolations for HTML DOM event attributes are disallowed. Please use the " +
2962+
"ng- versions (such as ng-click instead of onclick) instead.");
2963+
}
2964+
29592965
directives.push({
29602966
priority: 100,
29612967
compile: function() {
29622968
return {
29632969
pre: function attrInterpolatePreLinkFn(scope, element, attr) {
29642970
var $$observers = (attr.$$observers || (attr.$$observers = createMap()));
29652971

2966-
if (EVENT_HANDLER_ATTR_REGEXP.test(name)) {
2967-
throw $compileMinErr('nodomevents',
2968-
"Interpolations for HTML DOM event attributes are disallowed. Please use the " +
2969-
"ng- versions (such as ng-click instead of onclick) instead.");
2970-
}
2971-
29722972
// If the attribute has changed since last $interpolate()ed
29732973
var newValue = attr[name];
29742974
if (newValue !== value) {

test/ng/compileSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9524,17 +9524,17 @@ describe('$compile', function() {
95249524
// All interpolations are disallowed.
95259525
$rootScope.onClickJs = "";
95269526
expect(function() {
9527-
$compile('<button onclick="{{onClickJs}}"></script>')($rootScope);
9527+
$compile('<button onclick="{{onClickJs}}"></script>');
95289528
}).toThrowMinErr(
95299529
"$compile", "nodomevents", "Interpolations for HTML DOM event attributes are disallowed. " +
95309530
"Please use the ng- versions (such as ng-click instead of onclick) instead.");
95319531
expect(function() {
9532-
$compile('<button ONCLICK="{{onClickJs}}"></script>')($rootScope);
9532+
$compile('<button ONCLICK="{{onClickJs}}"></script>');
95339533
}).toThrowMinErr(
95349534
"$compile", "nodomevents", "Interpolations for HTML DOM event attributes are disallowed. " +
95359535
"Please use the ng- versions (such as ng-click instead of onclick) instead.");
95369536
expect(function() {
9537-
$compile('<button ng-attr-onclick="{{onClickJs}}"></script>')($rootScope);
9537+
$compile('<button ng-attr-onclick="{{onClickJs}}"></script>');
95389538
}).toThrowMinErr(
95399539
"$compile", "nodomevents", "Interpolations for HTML DOM event attributes are disallowed. " +
95409540
"Please use the ng- versions (such as ng-click instead of onclick) instead.");

0 commit comments

Comments
 (0)