版本前提:AngularJS 1.3 已放棄 IE8;1.2 仍支援 IE8,但核心團隊不再投入資源修復 IE8 及更早版本的問題。專案目前正式支援並嘗試修復的是 IE9 以上,CI 也在 IE9/IE10/IE11 上跑完整測試。IE8 及以下不在測試範圍,部分功能也許能動,但需要自行驗證。
1. IE7 及更早版本需 polyfill JSON.stringify,可用 JSON2 或 JSON3:
<!doctype html>
<html xmlns:ng="https://angularjs.org">
<head>
<!--[if lte IE 7]>
<script src="/path/to/json2.js"></script>
<![endif]-->
</head>
<body>...</body>
</html>
2. 在根元素加上 id="ng-app",同時使用 ng-app 屬性:
<html xmlns:ng="https://angularjs.org" id="ng-app" ng-app="optionalModuleName">
3. 避免使用自訂元素標籤,例如把 <ng:view> 改寫成屬性形式 <div ng-view>。
4. 若非得用自訂標籤,需在 IE8 及以下做兩件事——宣告命名空間,並提前 createElement:
<!--[if lte IE 8]>
<script>
document.createElement('ng-include');
document.createElement('ng-pluralize');
document.createElement('ng-view');
// 若要搭配 CSS 選擇器,再加這三行
document.createElement('ng:include');
document.createElement('ng:pluralize');
document.createElement('ng:view');
</script>
<![endif]-->
5. ng-style 的表達式在當時的版本於 Chrome/Firefox 可動,但 IE 11 及以下不行。
兩個最重要的部分:xmlns:ng 命名空間要為每個自訂標籤指定;document.createElement(yourTagName) 要對每個未定義的標籤提前建立,並用條件註解限定只在舊版 IE 執行。
my:)會被當成 XML 命名空間,必須有對應宣告 <html xmlns:my="ignored">。document.createElement('my-tag')。這些限制只針對「元素標籤名稱」,不適用於「屬性名稱」。所以像 <div my-tag your:tag></div> 這種寫法在 IE 中不需要任何特別處理。