【问题标题】:Angular $resource not working in IE11Angular $resource 在 IE11 中不起作用
【发布时间】:2015-03-19 16:03:38
【问题描述】:

我正在使用 Angular,并且正在使用 $resource 向 MVC 中的控制器发出请求。我遇到了一个问题,它在 IE11 中不起作用。适用于 chrome 和 firefox。

我有一个 homeController.js,它带有这个 $scope,当它到达 div 时会在 ng-init 上调用它

  $scope.getUserShortcuts = function () {
                    $scope.shortcuts = usersService.getUserShortcuts(11); 
                }

这调用了我的 userService.js

(function () {
    "use strict";

    var myAppModule = angular.module('myApp');

    myAppModule.factory('usersService', ['$resource', function ($resource) {
        console.log("load it");

        // user = controller, getusershortcuts = method, :userId = param
        var userShortcuts = $resource('/user/shortcuts/11', null,
        {
            'get': { method: 'GET', isArray:true }
        });

            getUserShortcuts: function (userId) {
                alert("test");
                return userShortcuts.query(userId);
            }

        };
    }]);
})();

我的警报被调用,但 IE11 甚至没有尝试进行 GET。

这是我的错误:

TypeError: Object.keys: argument is not an Object
   at extend (http://localhost:45669/Scripts/angular.js:412:7)
   at Resource[name] (http://localhost:45669/Scripts/angular-resource.js:574:13)
   at getUserShortcuts (http://localhost:45669/Scripts/app/services/userService.js:94:17)
   at $scope.getUserShortcuts (http://localhost:45669/Scripts/app/controllers/homeController.js:171:21)
   at $parseFunctionCall (http://localhost:45669/Scripts/angular.js:12331:7)
   at Scope.prototype.$eval (http://localhost:45669/Scripts/angular.js:14383:9)
   at pre (http://localhost:45669/Scripts/angular.js:23851:9)
   at invokeLinkFn (http://localhost:45669/Scripts/angular.js:8213:9)
   at nodeLinkFn (http://localhost:45669/Scripts/angular.js:7701:11)
   at compositeLinkFn (http://localhost:45669/Scripts/angular.js:7075:13) <div class="row" ng-init="getUserShortcuts(11)">
value= undefined

有谁知道问题出在哪里?

【问题讨论】:

  • 你有这方面的 plnkr/fiddle 吗?

标签: angularjs get internet-explorer-11


【解决方案1】:

假设userId 不是对象是安全的,但它必须是。此外,您的资源 URL 没有参数,所以我想知道您是否真的要传递 userId 或 URL 不正确。无论如何,正确的调用应该是这样的:

getUserShortcuts: function (userId) {
            alert("test");
            return userShortcuts.query({userId: userId});

IE 行为不同的原因是因为 Chrome 显然有一个 Object.keys 的 ES6 兼容实现,它将 userId 转换为其对象形式,而 ES5 兼容实现,如 IE 中的那个,抛出一个 TypeError。请注意,虽然 Chrome 没有报错,但并不代表你的代码没问题。

【讨论】:

  • 非常感谢您的帖子。我的方法现在有效,我不知道 IE 的这种行为。
猜你喜欢
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 2020-03-21
  • 2015-08-27
  • 2019-12-13
相关资源
最近更新 更多