mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-11 16:23:09 +00:00
fix bug where $eval on undefined throws error
This commit is contained in:
parent
4aac29da18
commit
9b392eca35
2 changed files with 19 additions and 8 deletions
12
src/Scope.js
12
src/Scope.js
|
|
@ -130,7 +130,8 @@ function createScope(parent, services, existing) {
|
||||||
$set: bind(instance, setter, instance),
|
$set: bind(instance, setter, instance),
|
||||||
|
|
||||||
$eval: function $eval(exp) {
|
$eval: function $eval(exp) {
|
||||||
if (exp === undefined) {
|
var type = typeof exp;
|
||||||
|
if (type == 'undefined') {
|
||||||
for ( var i = 0, iSize = evalLists.sorted.length; i < iSize; i++) {
|
for ( var i = 0, iSize = evalLists.sorted.length; i < iSize; i++) {
|
||||||
for ( var queue = evalLists.sorted[i],
|
for ( var queue = evalLists.sorted[i],
|
||||||
jSize = queue.length,
|
jSize = queue.length,
|
||||||
|
|
@ -138,18 +139,19 @@ function createScope(parent, services, existing) {
|
||||||
instance.$tryEval(queue[j].fn, queue[j].handler);
|
instance.$tryEval(queue[j].fn, queue[j].handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (typeof exp === 'function'){
|
} else if (type === 'function') {
|
||||||
return exp.call(instance);
|
return exp.call(instance);
|
||||||
} else {
|
} else if (type === 'string') {
|
||||||
return expressionCompile(exp).call(instance);
|
return expressionCompile(exp).call(instance);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
$tryEval: function (expression, exceptionHandler) {
|
$tryEval: function (expression, exceptionHandler) {
|
||||||
|
var type = typeof expression;
|
||||||
try {
|
try {
|
||||||
if (typeof expression == 'function') {
|
if (type == 'function') {
|
||||||
return expression.call(instance);
|
return expression.call(instance);
|
||||||
} else {
|
} else if (type == 'string'){
|
||||||
return expressionCompile(expression).call(instance);
|
return expressionCompile(expression).call(instance);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,11 @@ describe('scope/model', function(){
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('$eval', function(){
|
describe('$eval', function(){
|
||||||
|
var model;
|
||||||
|
|
||||||
|
beforeEach(function(){model = createScope();});
|
||||||
|
|
||||||
it('should eval function with correct this', function(){
|
it('should eval function with correct this', function(){
|
||||||
var model = createScope();
|
|
||||||
model.$eval(function(){
|
model.$eval(function(){
|
||||||
this.name = 'works';
|
this.name = 'works';
|
||||||
});
|
});
|
||||||
|
|
@ -30,18 +33,24 @@ describe('scope/model', function(){
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should eval expression with correct this', function(){
|
it('should eval expression with correct this', function(){
|
||||||
var model = createScope();
|
|
||||||
model.$eval('name="works"');
|
model.$eval('name="works"');
|
||||||
expect(model.name).toEqual('works');
|
expect(model.name).toEqual('works');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should do nothing on empty string and not update view', function(){
|
it('should do nothing on empty string and not update view', function(){
|
||||||
var model = createScope();
|
|
||||||
var onEval = jasmine.createSpy('onEval');
|
var onEval = jasmine.createSpy('onEval');
|
||||||
model.$onEval(onEval);
|
model.$onEval(onEval);
|
||||||
model.$eval('');
|
model.$eval('');
|
||||||
expect(onEval).wasNotCalled();
|
expect(onEval).wasNotCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should ignore none string/function', function(){
|
||||||
|
model.$eval(null);
|
||||||
|
model.$eval({});
|
||||||
|
model.$tryEval(null);
|
||||||
|
model.$tryEval({});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('$watch', function(){
|
describe('$watch', function(){
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue