作为一个 Angular 用户,我也对这个问题的标题不寒而栗,因为 IE8 是邪恶的化身,应该像疯狗一样被放下。
话虽这么说,我想知道是否还有其他人遇到过在 IE8 中加载 Angular 1.3 的问题,加载前页面中断并且只是报告错误:Object Expected
on an if condition使用 isArray() 函数。 (isArray() 也出现在 Angular 1.2 中,所以令我困惑的是它在那里工作但在 1.3 中不工作)
为了让大家明白我的原因,我公司最近采取了不再支持IE8新开发的步骤。但是我们的新 UI 需要在初始登录页面上仅支持 IE8,这样用户仍然可以访问我们支持 IE8 的旧软件。我希望我可以使用 1.3,并且只对着陆页进行一些小的调整,直到它也从 IE8 下消失。
首要问题:是否可以将 Angular 1.3 与 IE8 一起使用,或者在我们完全移除 IE8 支持之前我是否会被迫使用 1.2?
最佳答案
方法是有的,虽然有点粗糙。以下是您需要在 angular 和您的应用可能运行之前加载的代码。这是一个 shims/polyfill 的集合,大部分来自 Mozilla 开发者网络,一些是我做的。
请注意,这只允许 AngularJS 运行,它不会更新 IE8 的 JS 运行时。所以像somePromise.catch(...)
这样的东西是行不通的,你必须写成somePromise["catch"](...)
。
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement) {
if (this.length === 0) {
return -1;
}
var n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (isNaN(n)) {
n = 0;
} else if (n !== 0 && n !== Infinity && n !== -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= this.length) {
return -1;
}
var k = n >= 0 ? n : Math.max(this.length - Math.abs(n), 0);
while (k < this.length) {
if (k in this && this[k] === searchElement) {
return k;
}
++k;
}
return -1;
};
}
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun/*, thisArg*/) {
if (this === undefined || this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i];
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}
return res;
};
}
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
if (!Array.prototype.every) {
Array.prototype.every = function(callbackfn, thisArg) {
'use strict';
var T, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
if (typeof callbackfn !== 'function') {
throw new TypeError();
}
if (arguments.length > 1) {
T = thisArg;
}
k = 0;
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
var testResult = callbackfn.call(T, kValue, k, O);
if (!testResult) {
return false;
}
}
k++;
}
return true;
};
}
if (!Object.create) {
Object.create = (function() {
var Object = function() {};
return function (prototype) {
if (arguments.length > 1) {
throw new Error('Second argument not supported');
}
if (typeof prototype != 'object') {
throw new TypeError('Argument must be an object');
}
Object.prototype = prototype;
var result = new Object();
Object.prototype = null;
return result;
};
})();
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun /*, thisArg */) {
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; ++i) {
if (i in t)
fun.call(thisArg, t[i], i, t);
}
};
}
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/gm, '');
};
}
(function() {
//$http uses onload instead of onreadystatechange. Need shimming as IE8 doesn't have onload.
if (new XMLHttpRequest().onload === undefined) {
var orig = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function() {
var self = this;
if (!this.onreadystatechange && this.onload) {
this.onreadystatechange = function() {
if (self.readyState === 4) {
self.onload();
}
};
}
orig.apply(self, arguments);
};
}
})();
if (!Date.now) {
Date.now = function() {
return new Date().getTime();
};
}
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {
},
fBound = function() {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
if (!Object.keys) {
Object.keys = function(object) {
var keys = [];
for (var o in object) {
if (object.hasOwnProperty(o)) {
keys.push(o);
}
}
return keys;
};
}
if (!Object.getPrototypeOf) {
Object.getPrototypeOf = function(object) {
return object.__proto__ || object.constructor.prototype;
};
}
如果你有angular-bootstrap,你还需要给angular.min.js文件“打补丁”,因为angular-boostrap使用了{in: someCondition}
,但是因为比较老的JS运行时in
关键字是保留关键字,将在代码生成中失败。
查找:var e=(b?"s":'((l&&l.hasOwnProperty("'+a+'"))?l:s)')+"."+a;
替换:var e=(b?"s":'((l&&l.hasOwnProperty("'+a+'"))?l:s)')+"['"+a+"'] ";
关于javascript - AngularJS 1.3 页面不会在 IE8 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978533/