gpt4 book ai didi

javascript - 复制代码时找不到 AngularJS Controller

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:06 24 4
gpt4 key购买 nike

我正在 udemy 上制作尝试 AngularJS:初学者指南,但遇到了一个奇怪的问题。以下代码适用于类(class)管理员,但不适用于某些用户。

index.html

<html lang="en" ng-app='try'>
<head>
<script src="./js/angular.min.js"></script>

<script src="./js/app/app.module.js"></script>
<script src="./js/app/app.config.js"></script>

<script src="./js/app/blog-list/blog-list.module.js"></script>
<script src="./js/app/blog-list/blog-list.component.js"></script>
</head>

<body>
<div class='' ng-controller='BlogListController'></div>
</body>
</html>

app.module.js

'use strict';
angular.module('try', ['blogList']);

app.config.js

'use strict';
angular.module('try', [])
.config(function(){});

blog-list.module.js

'use strict';
angular.module('blogList', []);

blog-list.component.js

'use strict';
angular.module('blogList').
controller('BlogListController', function(){
console.log("hello")
});

我收到错误

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

但是如果在文件 blog-list.component.js 中我更改了模块名称:

'use strict';
angular.module('try'). <---- CHANGED THIS
controller('BlogListController', function(){
console.log("hello")
});

它有效。

为什么修复之前的代码对类(class)管理器有效?哪个才是真正的正确答案?

PS:所有内容都按照 index.html 文件规定的顺序加载。

提前致谢。

最佳答案

try加载模块时不依赖 blogList模块,因为app.config.js包含一个非常微妙的错误。

请注意 angular.module函数可以通过两种方式使用:检索模块和创建新模块。当提供第二个参数时,将创建一个新模块。第二个参数包含模块的依赖项。

这意味着该模块已加载到该应用程序中,如下所示:首先,angular.module('try', ['blogList'])创建一个新模块,名为try ,依赖于 blogList 。然后angular.module('try', []).config(function(){})创建一个名为 try 的新模块没有任何依赖项,覆盖之前的 try .

只需删除第二个参数,使 app.config.js 看起来像这样:

'use strict';
angular.module('try')
.config(function(){});

并且它应该始终有效。

关于javascript - 复制代码时找不到 AngularJS Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050679/

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