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

Commit c03ad24

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

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/ng/parse.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,9 @@ Parser.prototype = {
679679
return getter(self || object(scope, locals));
680680
}, {
681681
assign: function(scope, value, locals) {
682-
return setter(object(scope, locals), field, value, parser.text);
682+
var o = object(scope, locals);
683+
if (!o) object.assign(scope, o = {});
684+
return setter(o, field, value, parser.text);
683685
}
684686
});
685687
},
@@ -701,10 +703,11 @@ Parser.prototype = {
701703
return v;
702704
}, {
703705
assign: function(self, value, locals) {
704-
var key = indexFn(self, locals);
706+
var key = ensureSafeMemberName(indexFn(self, locals), parser.text);
705707
// prevent overwriting of Function.constructor which would break ensureSafeObject check
706-
var safe = ensureSafeObject(obj(self, locals), parser.text);
707-
return safe[key] = value;
708+
var o = ensureSafeObject(obj(self, locals), parser.text);
709+
if (!o) obj.assign(self, o = {});
710+
return o[key] = value;
708711
}
709712
});
710713
},

test/ng/parseSpec.js

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

10831099
describe('one-time binding', function() {

0 commit comments

Comments
 (0)