AngularJS - IE8 html模板

时间:2013-07-30 17:21:49

标签: angularjs

有没有人知道在IE8中使用AngularJS的样板模板。在文档中有一整节致力于让Angular与IE8一起工作,看起来所需的步骤非常具体,但到目前为止,我对ng-include

这样的命令几乎没有成功。

如果步骤非常具体,那么我假设有人在某处确认了可以与IE8一起使用的样板代码,如果可以共享,我们将不胜感激。至少如果它不起作用那么你至少知道你已经从已知的基线开始,并且可以更容易地分离问题。

2 个答案:

答案 0 :(得分:4)

我使用AngularJS创建了2个生产应用程序,只需几个javascript'修复'就能在IE8中完美运行。

首先,如果开发者控制台未打开,则console.log语句将失败。我在生成角度应用程序的初始页面上使用以下js片段修复了它:

 // Avoid `console` errors in browsers that lack a console.
            (function() {
                var method;
                var noop = function () {};
                var methods = [
                    'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
                    'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
                    'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
                    'timeStamp', 'trace', 'warn'
                ];
                var length = methods.length;
                var console = (window.console = window.console || {});

                while (length--) {
                    method = methods[length];

                    // Only stub undefined methods.
                    if (!console[method]) {
                        console[method] = noop;
                    }
                }
            }());

其次,我使用toISOString来转换日期时间戳。在IE中,该功能未实现,因此我使用此代码段:

 /*IE8 toISOString hack */
            if (!Date.prototype.toISOString) {
                Date.prototype.toISOString = function() {
                    function pad(n) { return n < 10 ? '0' + n : n }
                    return this.getUTCFullYear() + '-'
                        + pad(this.getUTCMonth() + 1) + '-'
                        + pad(this.getUTCDate()) + 'T'
                        + pad(this.getUTCHours()) + ':'
                        + pad(this.getUTCMinutes()) + ':'
                        + pad(this.getUTCSeconds()) + '.'
                        + pad(this.getUTCMilliseconds()) + 'Z';
                };
            }

第三,IE中不支持forEach方法,所以我使用它:

/*IE8 hack to support forEach */
            if (!Array.prototype.forEach) {
              Array.prototype.forEach = function(fn, scope) {
                for(var i = 0, len = this.length; i < len; ++i) {
                  fn.call(scope, this[i], i, this);
                }
              }
            }

所有这些代码片段都是从StackOverflow的答案中挖走的,而我的工作是YMMV。

我阅读了角度IE8文档,但我没有遇到文档中描述的任何情况。对于指令,我使用格式:<div directive-name>,一切正常。

答案 1 :(得分:1)

虽然这个问题应该更加详细,但我可以谈谈我过去为管理IE8代码所做的工作。

我让yeoman为我处理所有这些问题,而不是担心锅炉板和生产力杀戮程序。

<强> http://yeoman.io/

Yeoman是一位固执的助手,类似于Rail的rake,可以帮助您更快地构建应用程序。只需下载generator-angular和yeoman即可开始使用。

<强> https://github.com/yeoman/generator-angular

如果您不想将yeoman集成到您的产品中,请查看Yeoman如何制定IE8样板并使用测试应用程序复制它。

我希望这会有所帮助。