IE 11的角度范围回报函数,而不是对象

问题描述:

$scope.updateCart = function() { 

    item = $scope.productData; 

此代码段返回IE 11的功能,而不是对象。IE 11的角度范围回报函数,而不是对象

使用https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js

上一页标题:IE 11的jquery '参数不可选' 错误

以下代码:

dt = $.param({ 
      shopid: shop_id, 
      mtd: method, 
      item: item 
     }); 

抛出以下错误:

TypeError: Argument not optional 
    at add (https://code.jquery.com/jquery-1.9.1.js:7340:4) 
    at buildParams (https://code.jquery.com/jquery-1.9.1.js:7392:3) 
    at jQuery.param (https://code.jquery.com/jquery-1.9.1.js:7360:4) 
    at $scope.updateBACart (http://127.0.0.1:6636/js/baCartNg.js?v=1478430971:104:3) 
    at fn (Function code:2:216) 
    at expensiveCheckFn (http://code.angularjs.org/1.5.5/angular.js:15485:11) 
    at callback (http://code.angularjs.org/1.5.5/angular.js:25018:17) 
    at Scope.prototype.$eval (http://code.angularjs.org/1.5.5/angular.js:17229:9) 
    at Scope.prototype.$apply (http://code.angularjs.org/1.5.5/angular.js:17329:13) 
    at Anonymous function (http://code.angularjs.org/1.5.5/angular.js:25023:17) 

错误或者扔在IE 11而不是在铬

根据https://docs.angularjs.org/api/ng/service/ $ httpParamSerializerJQLike

我更改代码:

dt = $httpParamSerializerJQLike({ 
      shopid: shop_id, 
      mtd: method, 
      item: item 
     }); 

现在的代码不会引发错误,但请求是

item: %0Afunction+item()+%7B%0A++++%5Bnative+code%5D%0A%7D%0A 
    mtd: add 
    shopid: 1 

而不是项目数据。

+0

从去年片断的输出,它看起来像你的'item'变量实际持有一个*函数*。也许你只是忘了调用函数,例如像'var item = elements.item'应该是'var item = elements.item(0)'?尝试在该行前添加一个'console.log(item)',看看在浏览器的控制台中记录了什么。 –

+0

我注意到,这是我从NG范围得到的值 - 这在IE 11中是错误的(vs chrome) –

+0

奇怪的是IE和Chrome会有所不同。我认为你需要向我们展示更多的代码,这样其他人才能更好地理解你的变量发生了什么。 –

当然,你可以命名的item您的变量,而不是item1,并且会为现在的工作......但没有按告诉你什么是错误与你的原始代码。

The real问题是您将分配给全局变量而不是本地变量。当您使用:

function() { 
    item = (2 + 2); // or some other value 
} 

不使用var item在它之前,你实际上是分配给一个全球变量item。在浏览器方面,在全球范围内是window对象,因此上述等同于:

function() { 
    window.item = (2 + 2); 
} 

在Chrome中,没有任何问题:最初没有window.item,所以这个任务创建它。但在IE11中,window已具有item属性,并且它是只读!这意味着对全局item变量的所有分配都将被忽略,因此window.item始终具有其原始函数值。

您的“解决方案”通过使用不同的变量名称来解决此问题。一个更好,更强大,更高效的解决方案是使用局部范围变量来代替:

function() { 
    var item = (2 + 2); // does NOT create a global item variable 
} 

作为一个很好的做法,确保您总是使用var声明一个新的变量的时候,要避免意外创建全局变量。更好的是,为你的函数添加一个"use strict"指令,以便这些类型的赋值抛出错误而不是默默地忽略错误。

如果你真的需要创建一个全局变量,明确分配给属性的window对象本身:

function() { 
    window.globalItem = (2 + 2); 
} 

的解决方案是item1 = $scope.productData;代替item = $scope.productData;

出于某种原因,IE11设置itemfunction item() { [native code] }

+0

我明白发生了什么事。既然你使用'item = ...'而不是'var item = ...',你的代码试图分配一个**全局**变量'item'。在Chrome上,这不是问题,因为没有'window.item'。 IE似乎有一个'window.item'函数,它是**只读**。这意味着你使用'item = ...'的赋值实际上*失败*,稍后阅读'item'仍然会给你最初的'window.item'。简而言之,** real **问题是您的变量泄漏到全局范围 - 使用'var'! –