【问题标题】:AngularJS v1.4.4 'expected identifier' error on IE10IE10 上的 AngularJS v1.4.4 'expected identifier' 错误
【发布时间】:2015-11-20 21:41:36
【问题描述】:

我正在 VS2013 中创建一个带有 Angular JS 项目(角度版本 v1.4.4)的 MVC5。我一直在编码,一切都很好,然后突然我开始在启动时遇到这个错误:

http://localhost:63937/Scripts/angular.js 中第 5395 行第 34 列出现 JavaScript 严重错误\n\nSCRIPT1010:预期标识符

我只在 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 文件的引用。我可以发布一些代码,但它似乎甚至没有击中 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);
}

任何帮助将不胜感激。

【问题讨论】:

  • 我建议您使用版本1.3.18,因为它是最后一个稳定版本。截至今天,AngularJS 团队还没有发布稳定的1.4.x 版本。
  • @yvesmancera,你确定吗?在 Visual Studio 2013 NuGet Manager 中,它被列为稳定版本
  • 我的错,看起来你是对的,1.4.4 是一个稳定版本。
  • 我在 AngularJS 上找到了这个 issue,它说它会影响 IE8,但它也可能会影响 IE10。它不允许 catch 作为标识符,因为它是 ES3 上的保留字。

标签: javascript angularjs visual-studio-2013 asp.net-mvc-5 internet-explorer-10


【解决方案1】:

根据changelog看来,这是最近在v1.4.4中专门介绍的:

$animateCss:确保 skipBlocking 避免了先发制人的过渡延迟样式

为了进一步扩展在某些上下文中引发错误的行:

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 版本,直到这个错误被修复。

【讨论】:

  • 我尝试了 1.4.3 和 angular.min.js 刚刚抛出了一个新错误:SCRIPT5007: Object expected File: angular.min.js, Line: 7, Column: 238. 这是在 IE 中11.0.9600。还尝试了 1.6.1,在 .catch 处出现另一个错误。
【解决方案2】:

我在 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 声明是否简单:

<!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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2020-06-26
    • 1970-01-01
    • 2021-07-12
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多