名称为“的控制器未注册

时间:2018-05-13 16:40:40

标签: javascript angularjs asp.net-core

我在ASP.NET Core 2中使用AngularJs 1.6.9。 错误是

  

名称为“pag1Controller”的控制器未注册。

_Layout.cshtml:

<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="font-awesome/css/font-awesome.css" rel="stylesheet">
    <link href="~/css/ngDialog-master/css/ngDialog.css" rel="stylesheet">
    <link href="~/css/ngDialog-master/css/ngDialog-theme-default.min.css" rel="stylesheet">

    <script src="~/lib/angular-1.6.9/angular.js"></script>
    <script src="~/lib/angular-1.6.9/angular-route.js"></script>
    <script src="~/lib/ngDialog-master/js/ngDialog.js"></script>
    <script src="~/lib/angular-1.6.9/angular-cookies.min.js"></script>
    <script src="~/js/app/app.js"></script>

</head>

<body class="" ng-app="MyApp">

    <div id="wrapper">

        <nav class="navbar-default navbar-static-side" role="navigation">
            <div class="sidebar-collapse">
                <ul class="nav metismenu" id="side-menu">

                    <li>
                        <a href="index.html"><i class="fa fa-th-large"></i> <span class="nav-label">Pag:</span> <span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level collapse">
                            <li><a href="#!/Pag1">Pag1</a></li>
                        </ul>
                        <ul class="nav nav-second-level collapse">
                            <li><a href="#!/Pag2">Pag2</a></li>
                        </ul>
                    </li>

                </ul>

            </div>
        </nav>

        <div class="wrapper wrapper-content">
                @RenderBody()
        </div>
</body>
</html>

Home.cshtml:

@{
    ViewData["Title"] = "Home Page";
}

<div ng-view>

</div>

<div ng-controller="homeController">
    {{Message}}
</div>

<script src="~/js/app/controllers/homeController.js"></script>

app.js:

var app = angular.module("MyApp", ['ngCookies', 'ngDialog', 'ngRoute']);

app.config(function ($routeProvider) {

    config.baseURL = "http://localhost/";

    $routeProvider.when('/Pag1', {

        templateUrl: config.baseURL + "Pag1/Index",
        controller: 'pag1Controller'

    })

    $routeProvider.when('/Pag2', {
        templateUrl: config.baseURL + "Pag2/Index",
        controller: 'pag2Controller'
    })
});

pag1.cshtml

<div ng-controller="pag1Controller">

    Pag1

    <button ng-click="onMensaje()">View alert</button>

    <script type="text/ng-template" id="templateId">
        <h1>Template heading</h1>
        <p>Content goes here</p>
    </script>

</div>

<script src="~/js/app/controllers/pag1Controller/pag1Controller.js"></script>

pag1Controller.js:

app.controller('pag1Controller', function ($scope, ngDialog) {

    $scope.onMensaje = function () {

        ngDialog.open({ template: 'templateId' });

    };
});

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要将控制器包含在index.html文件中,以便控制器在加载应用程序时注册,

 <script src="~/js/app/app.js"></script>
 <script src="~/js/app/controllers/pag1Controller/pag1Controller.js"></script>