【问题标题】:Error: [ng:areq] Argument 'MainController' is not a function, got undefined in IE错误:[ng:areq] 参数“MainController”不是函数,在 IE 中未定义
【发布时间】:2016-11-07 19:42:26
【问题描述】:

我讨厌问这个问题 b/c 有很多类似的问题,但我今天浪费了 5 个小时试图解决导致此问题的原因。我的应用在 Chrome 和 Firefox 中运行完美,但在 IE 11 或 Edge 中,我收到 SCRIPT1006: Expected ')' MapService.js (43,55)SCRIPT1006: Expected ')' MainController.js (299,44)Error: [ng:areq] Argument 'MainController' is not a function, got undefined 错误。我的代码结构如下:

index.html

<html lang="en" ng-app="SS" ng-controller="MainController as ctrl" >

    <script src="src/index.js"></script>
    <script src="src/SS/DefaultConfigs.js"></script>
    <script src="src/SS/MapService.js"></script>
    <script src="src/SS/MainController.js"></script>
....

index.js

angular
    .module("SS", ["ngMaterial", "angular-intro"])
        .config(function($mdThemingProvider, $mdIconProvider) {
            "use strict";
            $mdIconProvider
                .defaultIconSet("./assets/svg/settings.svg", 128)
                .icon("clipboard", "./assets/svg/clipboard_48px.svg", 48)
                .icon("close", "./assets/svg/close_48px.svg", 48);
                ....

            $mdThemingProvider.theme("default")
                .primaryPalette("blue")
                .dark();
    });

MainController.js

angular
    .module('SS')

    .controller('MainController', function($interval, $location, $mdDialog, $mdSidenav, $mdToast, $scope, DefaultConfigs, MapService) {
        'use strict';
        var animationRunner;
        ....

        /* Snippet of code surrounding where 2nd error supposedly is */
        var updateMap = function() {
            MapService.updateCurrentRotation();
            MapService.updateCurrentZoom();
            MapService.updateCurrentCenter();
            $scope.setVisibility();
        };
        // line below is line 299
        $scope.setVisibility = function(ind=$scope.currentId, prev=$scope.prevId) {
            var temp = MapService.layers;
            if (layerExists(prev)) {
                temp[prev].setVisible(false);
            }
            if (layerExists(ind)) {
                temp[ind].setVisible(true);
            }
        };
        /* End snippet of code surrounding where 2nd error supposedly is */
    })

    .directive('ngRightClick', function($parse) {
        ....
    });

地图服务.js

angular
    .module('SS')

    .service('MapService', function($location, DefaultConfigs) {
        'use strict';
        this.map = null;
        this.layers = [];

        /* Snippet of code surrounding where 1st error supposedly is */
        this.createProjection = function() {
            this.proj = new ol.proj.Projection({
                code: 'EPSG:0000',
                units: 'pixels',
                extent: [0, 0, 8192, 8192]
            });
        };

        /* next line is line 43 */
        this.createLayer = function(ind, isVisible=false, counter=1) {
            var imageLayer = new ol.layer.Tile({
                preload: Infinity,
                source: new ol.source.XYZ({
                    url: this.baseURL + ind + '/{z}-{y}-{x}.png',
                    projection: this.proj,
                    wrapX: false,
                    attributions: [this.tileAttribution]
                }),
                transitionEffect: 'resize',
                visible: isVisible
            });
            imageLayer.id = counter;
            imageLayer.playerId = parseInt(ind, 10);

            return imageLayer;
        };
        /* End snippet of code surrounding where 1st error supposedly is */
    });

我检查了 JSLint 以检查我的代码中的错误,但没有发现任何东西可以修复 IE 错误。我没有使用全局控制器声明,我没有调用 ng-app 两次,我相信我的 javascript 调用顺序正确,我没有覆盖我的 SS 模块,我没有发现任何逗号或括号错误。我难住了。我已经阅读了许多其他解决方案,并且检查了清单here 上的所有选项。

这两个) 错误一直显示并且仅在 IE 中发生的事实是否暗示了我的问题?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    我认为您的问题是默认参数:

    ind=$scope.currentId, prev=$scope.prevId
    ...
    isVisible=false, counter=1
    

    这是ES6 feature,根据compatability chart,IE11 和 Edge 仅部分支持 ES6。在 Edge 14 之前不支持它。

    您可以更改代码、使用转译器、使用ES6 shim,或者(我最喜欢的)放弃对 Internet Explorer 的支持。

    【讨论】:

    • 谢谢,这解决了问题!我以为某处存在兼容性问题,但是哇……默认参数感觉很正常,我认为这不是原因。不幸的是,我需要将此项目测试回 IE9 ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2015-11-03
    • 2016-10-23
    • 2015-10-10
    • 2016-11-12
    • 2016-03-04
    • 2015-11-18
    相关资源
    最近更新 更多