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

Commit 9b392ec

Browse files
committed
fix bug where $eval on undefined throws error
1 parent 4aac29d commit 9b392ec

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Scope.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,28 @@ function createScope(parent, services, existing) {
130130
$set: bind(instance, setter, instance),
131131

132132
$eval: function $eval(exp) {
133-
if (exp === undefined) {
133+
var type = typeof exp;
134+
if (type == 'undefined') {
134135
for ( var i = 0, iSize = evalLists.sorted.length; i < iSize; i++) {
135136
for ( var queue = evalLists.sorted[i],
136137
jSize = queue.length,
137138
j= 0; j < jSize; j++) {
138139
instance.$tryEval(queue[j].fn, queue[j].handler);
139140
}
140141
}
141-
} else if (typeof exp === 'function'){
142+
} else if (type === 'function') {
142143
return exp.call(instance);
143-
} else {
144+
} else if (type === 'string') {
144145
return expressionCompile(exp).call(instance);
145146
}
146147
},
147148

148149
$tryEval: function (expression, exceptionHandler) {
150+
var type = typeof expression;
149151
try {
150-
if (typeof expression == 'function') {
152+
if (type == 'function') {
151153
return expression.call(instance);
152-
} else {
154+
} else if (type == 'string'){
153155
return expressionCompile(expression).call(instance);
154156
}
155157
} catch (e) {

test/ScopeSpec.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,36 @@ describe('scope/model', function(){
2121
});
2222

2323
describe('$eval', function(){
24+
var model;
25+
26+
beforeEach(function(){model = createScope();});
27+
2428
it('should eval function with correct this', function(){
25-
var model = createScope();
2629
model.$eval(function(){
2730
this.name = 'works';
2831
});
2932
expect(model.name).toEqual('works');
3033
});
3134

3235
it('should eval expression with correct this', function(){
33-
var model = createScope();
3436
model.$eval('name="works"');
3537
expect(model.name).toEqual('works');
3638
});
3739

3840
it('should do nothing on empty string and not update view', function(){
39-
var model = createScope();
4041
var onEval = jasmine.createSpy('onEval');
4142
model.$onEval(onEval);
4243
model.$eval('');
4344
expect(onEval).wasNotCalled();
4445
});
46+
47+
it('should ignore none string/function', function(){
48+
model.$eval(null);
49+
model.$eval({});
50+
model.$tryEval(null);
51+
model.$tryEval({});
52+
});
53+
4554
});
4655

4756
describe('$watch', function(){

0 commit comments

Comments
 (0)