Skip to content

Commit 6dd93c1

Browse files
committed
fix($parse): correctly assign expressions who's path is undefined and that use brackets notation
Closes angular#8039
1 parent 36831ec commit 6dd93c1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ng/parse.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,9 @@ Parser.prototype = {
683683
return getter(self || object(scope, locals));
684684
}, {
685685
assign: function(scope, value, locals) {
686-
return setter(object(scope, locals), field, value, parser.text);
686+
var o = object(scope, locals);
687+
if (!o) object.assign(scope, o = {});
688+
return setter(o, field, value, parser.text);
687689
}
688690
});
689691
},
@@ -708,6 +710,7 @@ Parser.prototype = {
708710
var key = indexFn(self, locals);
709711
// prevent overwriting of Function.constructor which would break ensureSafeObject check
710712
var safe = ensureSafeObject(obj(self, locals), parser.text);
713+
if (!safe) obj.assign(self, safe = {});
711714
return safe[key] = value;
712715
}
713716
});

test/ng/parseSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,22 @@ describe('parser', function() {
10771077
fn.assign(scope, 123);
10781078
expect(scope).toEqual({a:123});
10791079
}));
1080+
1081+
it('should expose working assignment function for expressions ending with brackets', inject(function($parse) {
1082+
var fn = $parse('a.b["c"]');
1083+
expect(fn.assign).toBeTruthy();
1084+
var scope = {};
1085+
fn.assign(scope, 123);
1086+
expect(scope.a.b.c).toEqual(123);
1087+
}));
1088+
1089+
it('should expose working assignment function for expressions with brackets in the middle', inject(function($parse) {
1090+
var fn = $parse('a["b"].c');
1091+
expect(fn.assign).toBeTruthy();
1092+
var scope = {};
1093+
fn.assign(scope, 123);
1094+
expect(scope.a.b.c).toEqual(123);
1095+
}));
10801096
});
10811097

10821098

0 commit comments

Comments
 (0)