Argument' SearchController'不是一个功能,未定义

时间:2016-08-10 18:32:47

标签: angularjs typescript

这是我的代码,大大简化了, 我的问题是 - 为什么我收到此错误消息?

  

[ng:areq] Argument' SearchController'不是一个功能,未定义

     

http://errors.angularjs.org/1.5.8/ng/areq?p0=SearchController&p1=not%20aNaNunction%2C%20got%20undefined

index.cshtml

<!DOCTYPE html>
<html ng-app="app">
    <head>
        <script src="~/Components/angular/angular.min.js"></script>
        <script src="~/Scripts/Common/app.js"></script>
    </head>
    <body>
        <div ng-view></div>
    </body>
</html>

app.ts

namespace App {

    export let app = angular.module("app", ["ngRoute", "ngSanitize"]);

    // Defined custom routes for using "ng-view" directive, 
    // enable single page application (SPA) functionality

    app.config(function ($routeProvider) {
        $routeProvider
            .when("/", {
                templateUrl: "/RealSuiteApps/RealForm/-1/MyForm/Search/",
                controller: "SearchController",
                controllerAs: "vm"
            })
            .when("/Detail/:id", {
                templateUrl: "/RealSuiteApps/RealForm/-1/MyForm/Detail/",
                controller: "DetailController",
                controllerAs: "vm"
            })
            .otherwise({ redirectTo: "/" });
    });
}

search.cshtml - 通过调用上面定义的templateUrl返回的HTML:/ RealSuiteApps / RealForm / -1 / MyForm / Search /(此演示简化)

<script src="~/Scripts/MyForm/search.controller.js"></script>

<div class="container">

    <h1>Hello from Search Controller</h1>

</div>

控制器 search.controller.js

namespace App {

    /**
     * SearchController
     */
    class SearchController {

        static $inject: string[] = ["DataService"];

        constructor(private dataService: DataService) { }

    }

    app.controller("SearchController", SearchController);
}

1 个答案:

答案 0 :(得分:1)

看起来您只在搜索视图中加载search.controller.js文件,但该路由甚至在此之前实例化控制器,如果您尚未加载脚本,则他无法执行此操作。

解决方案是在应用的开头加载它。