我正在 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/