Angular $q .catch() 方法在 IE8 中失败 [英] Angular $q .catch() method fails in IE8

查看:20
本文介绍了Angular $q .catch() 方法在 IE8 中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 IE8 上尝试捕获承诺拒绝时遇到一个奇怪的错误(由基本的 ngResource 调用返回的承诺):

I'm experiencing a weird bug on IE8 while trying to catch a promise reject (promise returned by a basic ngResource call) :

此代码使用 .then(success, fail) 语法:

promise.then(function(response) {
  // success
},
function(response) {
  // error
});

但是这个失败了 .then(success).catch(fail) 语法:

but this one fails with .then(success).catch(fail) syntax :

promise.then(function(response) {
  // success
})
.catch(function(response) {
  // error
});

指向 .catch() 行的 IE 错误是:

and the IE error pointing to the .catch() line is :

预期标识符

我做错了吗?有人复制吗?还是由于关键字限制导致的常见IE8?

Am I doing something wrong ? someone reproduce it ? or is it a common IE8 due to restricted keyword ?

谢谢

推荐答案

需要使用括号表示法:

promise.then(function(response) {
  // success
})
["catch"](function(response) {
  // error
});

这是因为 IE8 实现的 ECMAScript 3 不允许使用点符号的裸关键字.现代浏览器实现了允许它的 ECMAScript 5.

This is because IE8 implements ECMAScript 3 that does not allow bare keywords in dot notation. Modern browsers implement ECMAScript 5 that allows it.

许多库将 .catch 别名为另一个关键字.然而,Angular 承诺的构建方式并不容易扩展 $q 承诺.所以 ["catch"] 必须要做.请注意,对于 finally 也是如此.

A lot of libraries alias .catch with another keyword. However, the way Angular promises are built it is not simple to extend $q promises. So ["catch"] would have to do. Note this is also true for finally.

这篇关于Angular $q .catch() 方法在 IE8 中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆