AngularJS v1.4.4 'expected identifier' IE10 错误

AngularJS v1.4.4 'expected identifier' error on IE10

我正在 VS2013 中使用 Angular JS 项目(angular 版本 v1.4.4)创建一个 MVC5。我一直在编码,一切都很好,然后突然开始在启动时出现此错误:

JavaScript critical error at line 5395, column 34 in http://localhost:63937/Scripts/angular.js\n\nSCRIPT1010: Expected identifier

我只在 IE10 上遇到这个错误,它在 Chrome 上工作正常。

我的引用在布局文件中,如下所示:

<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/Site.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/angular.js"></script> 
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/angular-route.js"></script>

HTML 页面包含对 App.js 文件的引用。我可以 post 一些代码,但它似乎甚至没有命中 App.JS 文件。有人 运行 了解这个问题,或者对我应该从哪里开始寻找这个问题有任何建议吗?据我所知,我将我的代码恢复到它在 IE10 中工作的状态,但它仍然不会 运行.

这是 angular.js 文件中包含错误行号的特定函数。具体行在'catch'函数中的return语句中:

getPromise: function() {
  if (!this.defer) {
    this.defer = $q.defer();
  }
  return this.defer.promise;
},
then: function(f1,f2) {
  return this.getPromise().then(f1,f2);
},
'catch': function(f1) {
  return this.getPromise().catch(f1);
},
'finally': function(f1) {
  return this.getPromise().finally(f1);
}

如有任何帮助,我们将不胜感激。

根据 changelog:

看来这是最近在 v1.4.4 中专门介绍的

$animateCss: make sure that skipBlocking avoids the pre-emptive transition-delay styling

进一步扩展针对某些上下文抛出错误的行:

var $CoreAnimateCssProvider = function() {
  this.$get = ['$$rAF', '$q', function($$rAF, $q) {

    var RAFPromise = function() {};
    RAFPromise.prototype = {
      done: function(cancel) {
        this.defer && this.defer[cancel === true ? 'reject' : 'resolve']();
      },
      end: function() {
        this.done();
      },
      cancel: function() {
        this.done(true);
      },
      getPromise: function() {
        if (!this.defer) {
          this.defer = $q.defer();
        }
        return this.defer.promise;
      },
      then: function(f1,f2) {
        return this.getPromise().then(f1,f2);
      },
      'catch': function(f1) {
        return this.getPromise().catch(f1);
      },
      'finally': function(f1) {
        return this.getPromise().finally(f1);
      }
    };
//... more code

$CoreAnimateCssProvider 未包含在 1.4.3 中,因此绝对是新的。

我的建议是您暂时尝试 1.4.3 版本,直到修复此错误。

我在 IE 11.0.9600 中遇到了类似的问题。我尝试了 1.4.3 并且 angular.min.js 只是抛出了一个新错误:

SCRIPT5007: Object expected 
File: angular.min.js, Line: 7, Column: 238.

还尝试了 1.6.1,它在 js 中的另一个“.catch”处引发了另一个错误。

但是,此错误也可能是由于文档类型被搞砸以及 IE 试图以旧模式解析内容造成的。要修复,请添加到您的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=10" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration> 

或者,在您的主布局页面中添加:

<meta http-equiv="X-UA-Compatible" content="IE=11" />

并检查您的文档类型声明是否简单:

<!doctype html>

附带说明一下,在 IE 浏览器(即使是最新的浏览器)中使用 angularjs 还有其他几个问题,归结为使用:

  1. ng-style 标签而不是 style="{{ someCss }}"
  2. ng-attr-type 标签而不是按钮中的 type="{{ someExpression }}"
  3. ng-attr-value 标签而不是 value="{{ someExpression}}"
  4. ng-attr-placeholder 标签而不是 placeholder="{{ someExpression }}"

IE compatibility with angular