2017-01-30 174 views
1

我有一個AngularJS應用程序,它給了我一些Internet Explorer 11中的問題 - 在我的管理區域中,我得到的控制檯日誌錯誤似乎與某些問題有關我注意到在頁面中使用的Internet Explorer(11版專),但是在Chrome /火狐等精細過濾時數據對象不支持屬性或方法'findIndex'IE11 javascript問題

Object doesn't support property or method 'findIndex' 
at Anonymous function (http://myapp.local/js/controllers/admin/UsersController.js:363:9) 

當我瀏覽到該行的代碼,這是有問題的部分: -

[363] var indexInOriginalSet = $scope.originalSet.findIndex(function(u) { 
[364]  return u.id == userId; 
[365] }); 

什麼是用findIndex解決這個IE問題的最佳解決方案?

回答

2

可以使用填充工具,在partirular這一個:

// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex 
if (!Array.prototype.findIndex) { 
    Object.defineProperty(Array.prototype, 'findIndex', { 
    value: function(predicate) { 
    // 1. Let O be ? ToObject(this value). 
     if (this == null) { 
     throw new TypeError('"this" is null or not defined'); 
     } 

     var o = Object(this); 

     // 2. Let len be ? ToLength(? Get(O, "length")). 
     var len = o.length >>> 0; 

     // 3. If IsCallable(predicate) is false, throw a TypeError exception. 
     if (typeof predicate !== 'function') { 
     throw new TypeError('predicate must be a function'); 
     } 

     // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. 
     var thisArg = arguments[1]; 

     // 5. Let k be 0. 
     var k = 0; 

     // 6. Repeat, while k < len 
     while (k < len) { 
     // a. Let Pk be ! ToString(k). 
     // b. Let kValue be ? Get(O, Pk). 
     // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). 
     // d. If testResult is true, return k. 
     var kValue = o[k]; 
     if (predicate.call(thisArg, kValue, k, o)) { 
      return k; 
     } 
     // e. Increase k by 1. 
     k++; 
     } 

     // 7. Return -1. 
     return -1; 
    } 
    }); 
} 

你可以找到更多的細節here

+0

歡呼如果你需要更多的信息只是問我給那一槍 – Zabs

+0

;) – rick

+0

我強烈反對擴大諮詢內置插件。相反,你應該使用從'Array.prototype'繼承的自己的'prototype'。 – NonPolynomial

3

我寫了一個小功能,要做到這一點,你想要什麼。它需要一個數組作爲第一個參數,而filter -callback作爲第二個參數。

var findIndex = function(arr, fn) { 
    return arr.reduce(function(carry, item, idx) { 
     if(fn(item, idx)) { 
      return idx; 
     } 

     return carry; 
    } , -1); 
}; 

console.log(findIndex(arr, function(u) { 
    return u.id == userId; 
})); 
0

對於那些誰在他們的角度得到這個錯誤(> = 2)在IE瀏覽器的應用程序,如果你使用的角度CLI中創建應用程序,你會發現文件polyfills.ts在src目錄或什麼是通過角CLI創建的源文件的根目錄下,在polyfills.ts找到並取消以下import語句:

/** IE9, IE10 and IE11 requires all of the following polyfills. **/ 
// import 'core-js/es6/symbol'; 
// import 'core-js/es6/object'; 
// import 'core-js/es6/function'; 
// import 'core-js/es6/parse-int'; 
// import 'core-js/es6/parse-float'; 
// import 'core-js/es6/number'; 
// import 'core-js/es6/math'; 
// import 'core-js/es6/string'; 
// import 'core-js/es6/date'; 
// import 'core-js/es6/array'; 
// import 'core-js/es6/regexp'; 
// import 'core-js/es6/map'; 
// import 'core-js/es6/weak-map'; 
// import 'core-js/es6/set'; 
相關問題