Javascript 名称为“mainController”的控制器未注册

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42084926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:53:10  来源:igfitidea点击:

The controller with the name 'mainController' is not registered

javascriptangularjs

提问by engr_ejo

i have this code in my script.js file

我的 script.js 文件中有此代码

var mainController = function($scope){
  $scope.message = "Plunker";
};

and this is my HTML

这是我的 HTML

<!DOCTYPE html>
<html ng-app>
    <head>
        <script data-require="[email protected]" data-semver="1.6.1"    src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js"></script>
    </head>

    <body ng-controller="mainController">
        <h1>Hello {{ message }}</h1>
    </body>
</html>

i declared ng-app in the opening html tag

我在开始的 html 标签中声明了 ng-app

but i get this error on my console that mainController is not registered

但我在我的控制台上收到此错误,即 mainController 未注册

采纳答案by Mawg says reinstate Monica

To paraphrase http://www.w3schools.com/angular/angular_modules.asp

转述http://www.w3schools.com/angular/angular_modules.asp

 <div ng-app="myApp" ng-controller="mainController">
{{ firstName + " " + lastName }}
</div>

<script>

var app = angular.module("myApp", []);

app.controller("mainController", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

</script> 

The important line is

重要的一行是

    app.controller("mainController", function($scope) 

which injects your controller into your app

将您的控制器注入您的应用程序

回答by georgeawg

The code is following an obsolete example.

该代码遵循一个过时的示例。

Migrating from 1.2 to 1.3

Controllers Due to 3f2232b5, $controller will no longer look for controllers on window. The old behavior of looking on window for controllers was originally intended for use in examples, demos, and toy apps. We found that allowing global controller functions encouraged poor practices, so we resolved to disable this behavior by default.

To migrate, register your controllers with modules rather than exposing them as globals:

Before:

function MyController() {
  // ...
}

After:

angular.module('myApp', []).controller('MyController', [function() {
  // ...
}]);

-- AngularJS Developer Guide -- Migrating from 1.2 to 1.3

从 1.2 迁移到 1.3

控制器 由于3f2232b5, $controller 将不再在窗口上寻找控制器。在窗口上查看控制器的旧行为最初旨在用于示例、演示和玩具应用程序。我们发现允许全局控制器功能会鼓励不良做法,因此我们决定默认禁用此行为。

要迁移,请使用模块注册您的控制器,而不是将它们公开为全局变量:

前:

function MyController() {
  // ...
}

后:

angular.module('myApp', []).controller('MyController', [function() {
  // ...
}]);

-- AngularJS 开发者指南 -- 从 1.2 迁移到 1.3

回答by Saurabh Agrawal

You need to register your controller with the main module of your application.

您需要在应用程序的主模块中注册您的控制器。

Try this in your app.js

在你的 app.js 中试试这个

 var myApp = angular.module('app', []);
 myApp.controller('mainController', function($scope){
    $scope.message = "Plunker";     
 });

and in your html

并在您的 html 中

<!DOCTYPE html>
<html ng-app="app">
    <head>
        <script data-require="[email protected]" data-semver="1.6.1"    src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js"></script>
    </head>

    <body ng-controller="mainController">
        <h1>Hello {{ message }}</h1>
    </body>
</html>

回答by Kanya Ravi

You need to register your controller with as like this in your script.js file

你需要像这样在你的 script.js 文件中注册你的控制器

var app = angular.module('myApp', []);
 app.controller('mainController', function($scope) {
  $scope.message= "msg";

   });