gpt4 book ai didi

angularjs - 具有此名称的 Controller 未在 angularjs ngroute 中注册

转载 作者:行者123 更新时间:2023-12-02 23:01:59 25 4
gpt4 key购买 nike

我正在 mvc-angularjs 中创建一个 Web 应用程序,我正在使用 ngroute。

我的布局中的 Angularjs Controller 带有 html 代码:

<div class="container body-content" ng-app="MyApp">
<br />
<br />
<br />
<div style="float:right;">
<a href="#" class="btn btn-danger">Sign Out</a>
</div>

@RenderBody()
<hr />
</div>

<script>
(function () {

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

app.controller('myctrllayout', function ($scope) {

});
})();
</script>

这是我的欢迎页面:

<div ng-controller="welcome">

</div>
<script>
angular.module('MyApp',[])
.controller('welcome', function ($scope) {

});
</script>

当我运行代码时,出现以下错误

Error: $controller:ctrlreg A controller with this name is not registered.

The controller with the name 'welcome' is not registered.

当我将应用从 ('MyApp',[]) 更改为 ('MyApp') 时:

Module 'MyApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

最佳答案

angular.module 可以创建新模块或检索现有模块。这取决于您使用的语法。

这也是referenced官方 Angular 文档上。

例如

angular.module('MyApp',[]) 创建一个新模块,因为它包含依赖项注入(inject)数组(作为第二个参数)。

angular.module('MyApp') 是对现有模块的引用,因为它不包含依赖项注入(inject)数组。

在您的情况下,它会在第一个代码块上创建一个新模块。因此,在第二个代码块中,您需要检索它。

如果你改变的话,问题可能会得到解决

第二个代码块中的

angular.module('MyApp',[])angular.module('MyApp')

关于angularjs - 具有此名称的 Controller 未在 angularjs ngroute 中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42244981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com