【问题标题】:Angular JS & TypeScript - Error: ng:areq Bad Argument "Argument 'XXXXXX' is not a function, got undefined"Angular JS & TypeScript - 错误:ng:areq Bad Argument “Argument 'XXXXXX' is not a function, got undefined”
【发布时间】:2015-06-27 13:03:41
【问题描述】:

不断收到标题中指定的错误。

我将模型和控制器拆分为单独的文件,分别存储在模型和控制器目录下。

当我尝试将它们链接起来时,我收到一条错误消息“ng:areq Bad Argument “Argument 'rightPaneCtrl' is not a function, got undefined”。

我的代码有什么问题?

index.html↓

<html ng-app="rightpaneMVC" data-framework="typescript">
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
    <!--<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.1.min.js"></script>
    <script src="//code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>-->
    <style>
        .item_list {
            display: flex;
            flex-direction: row;
            padding-left: 0;
        }

            .item_list > li {
                list-style-type: none;
                cursor: pointer;
                border: 1px solid black;
                padding: 0.3rem;
                width: 100px;
                margin-right: 0.2rem;
            }
    </style>

    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css">
</head>
<body>

    <div id="tab-category" ng-controller="rightPaneCtrl">
        <!--<ul>
            <li ng-repeat="tab in vm.tabs track by $index" ng-click="vm.handleTabClick(tab,$index)">
                <a href="#{{ tab.tabID }}">{{ tab.name }}{{ tab.tabID }}</a>
                <span ng:class="{true:'ui-icon ui-icon-circle-close ui-closable-tab', false:''}[$index!=0]"></span>
            </li>
        </ul>-->
        <hr />
        <tabset ng-init="startActive = true">
            <tab ng-repeat="tab in vm.tabs track by $index" active="startActive">
                <tab-heading ng-switch="tab.isClosable">
                    <div ng-switch-when=true>
                        {{ tab.name }} <a ng-click="vm.handleTabClick($index)" href=''><i class="icon-remove"></i></a>
                    </div>
                    <div ng-switch-when=false>
                        {{ tab.name }}
                    </div>
                </tab-heading>
                <ul class="item_list" ng-switch="tab.categoryTypeString">

                    <li ng-switch-when="DAI" ng-repeat="item in tab.items" ng-click="vm.handleItemClick(item)">
                        <img ng-src="{{item.thumbnailPath}}"><br />
                        <p align="center">{{ item.categoryName }}</p>
                    </li>
                    <li ng-switch-when="CHU" ng-repeat="item in tab.items" ng-click="vm.handleItemClick(item)">
                        <img ng-src="{{item.thumbnailPath}}"><br />
                        <p align="center">{{ item.categoryName }}</p>
                    </li>
                    <li ng-switch-default ng-repeat="item in tab.items">
                        <img ng-src="{{item.thumbnailPath}}"><br />
                        <p align="center">{{ item.categoryName }}</p>
                    </li>
                </ul>
            </tab>
        </tabset>


        <!--<div ng-repeat="tab in vm.tabs track by $index" id="{{ tab.tabID }}">

                <ul class="item_list" ng-switch="tab.categoryTypeString">
                    <li ng-switch-when="DAI" ng-repeat="item in tab.items" ng-click="vm.handleItemClick(item)"> {{ item.categoryName }}</li>
                    <li ng-switch-when="CHU" ng-repeat="item in tab.items" ng-click="vm.handleItemClick(item)"> {{ item.categoryName }}</li>
                    <li ng-switch-default ng-repeat="item in tab.items"> {{ item.categoryName }}</li>
                </ul>
            </div>-->
    </div>

    <script src="//code.angularjs.org/1.4.1/angular.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.4.0/ui-bootstrap-tpls.min.js"></script>
    <script src="Application.js"></script>
    <script src="controllers/RightPaneCtrl.js"></script>
</body>
</html>

Application.ts↓

module rightpane {

    'use strict';

    angular.module('rightpaneMVC', ['ui.bootstrap']);
    angular.module('rightpaneMVC').controller('rightPaneCtrl', RightPaneCtrl);
} 

RightPaneCtrl.ts↓

 module rightpane {

        'use strict';

        export class RightPaneCtrl {        

            public static $inject = ['$scope', '$http'];        

            private mModel = new RightPaneTabList;        

            public constructor(

                private $scope: any,
                private $http: ng.IHttpService) {

                $scope.vm = this;        
            }        

            public get tabs(): Tab[] {
                return this.mModel.tabs;
            }        

            handleItemClick(aItem: Item): void {
                console.log('Item clicked');
                this.mModel.addCategory(aItem);
            }        

            handleTabClick(tabIndex: number): void {
                console.log('Tab clicked');
                this.mModel.tabs.splice(tabIndex, 1);
            }        
        }        
    } 

目录结构↓

root
    |-index.html
    |-controllers
        |-RightPaneCtrl.ts

【问题讨论】:

标签: javascript angularjs typescript


【解决方案1】:

所以,我们在the other answer 方面取得了一些进展。但是现在出现了新问题,OP 创建了这个 plunker

http://plnkr.co/edit/WLH1GgLlpE7nek1poWnA?p=preview

带有此错误消息:

TypeError: rightpane.RightPaneTabList 不是函数 在新的 RightPaneCtrl (RightPaneCtrl.js:14) ...

有更新和partially working version。问题是......缺少加载到浏览器中的组件......当我们创建这样的文档时

枚举/ECategoryType.js 模型/Item.js 模型/Tab.js 模型/RightPaneTabList.js 控制器/RightPaneCtrl.js 应用程序.js

我们必须将它们全部(并以正确的顺序)附加到页面代码中:

<script src="enums/ECategoryType.js"></script>
<script src="models/Item.js"></script>
<script src="models/Tab.js"></script>
<script src="models/RightPaneTabList.js"></script>
<script src="controllers/RightPaneCtrl.js"></script>
<script src="Application.js"></script> 

(图像丢失,但应用应该启动)

【讨论】:

  • 您解决了问题!现在一切顺利...非常感谢!!!
  • 太棒了!很高兴看到这一点,使用 Typescript 享受强大的 Angular... 不错的选择 ;)
【解决方案2】:

有两个问题。有a working example,显示了下面的变化(简化版,只是为了让它运行)

首先,如果我们想使用某个类..它已经被加载了,所以我们必须改变这些脚本的顺序

// here we must make JS aware about our Ctrl
<script src="controllers/RightPaneCtrl.js"></script>
<script src="Application.js"></script> 
// here it would be too late
// <script src="controllers/RightPaneCtrl.js"></script>

而且,因为我们的控制器有模块/命名空间:

module rightpane {        
    export class RightPaneCtrl {
    ...

我们必须为其指定全名:

angular.module('rightpaneMVC')
    // instead of this
    // .controller('rightPaneCtrl', RightPaneCtrl);
    // wee need this
    .controller('rightPaneCtrl', rightpane.RightPaneCtrl);

查看here

【讨论】:

  • 您的解决方案似乎已经解决了问题,但现在又出现了另一个错误,提示“TypeError: undefined is not a function at new RightPaneCtrl”。我只是修改了您指出的位,而其他代码未受影响...
  • 好吧,您可以更改我的 plunker 以显示问题,或者您应该在这里定义新问题...因为我已尽力帮助您根据当前问题找出问题所在...我想说...另一个错误是关于另一个问题的,对吗?但是如果你能告诉我我的 plunker 中的错误......我准备好提供帮助了。如果您创建新问题..您的新问题将获得更多受众..这有意义吗?
  • 我在 Plunker 上复制了我的代码。我是这个工具的新手,不确定我是否在网站上正确创建了文件...请看一下。Link
  • 你做得很好......很高兴见到你的小伙子......我会试着检查一下,但不能保证时间......会尽快来找你
  • 感谢您的帮助!