gpt4 book ai didi

javascript - 当 Controller 在单独的文件中时应用程序不加载

转载 作者:行者123 更新时间:2023-11-30 15:43:42 24 4
gpt4 key购买 nike

这是对我的问题的补充:AngularJS Doesn't Resolve An Array of Objects

正如我在最终编辑中所述:

I have resolved this by placing controller in the same file where app is defined - app.js. When placed in separate folder it is not working. I do not know why at this point.

但这是为什么呢?我已经尝试了几次,当 Controller 位于单独的文件 - js/controller.js 中时,应用程序不会加载。并且我检查了路径,是正确的。

这是应用程序:

这是我的 HTML:

<body data-ng-app="App">
*
*
*

<div ng-controller="Ctrl">
<p>{{obj.desc}}</p>
</div>

*
*
*
</body>

应用程序.js

var app = angular.module('App', []);

Controller .js

app.controller('Ctrl', function($scope) {
$scope.obj = [
{
intro: "intromessage",
desc: "desc"
},
{
intro: "intromessage2",
desc: "desc"
}
];
});

最佳答案

假设 controller.js 已正确加载。您需要在新文件中定义 var app:

var app = angular.module('App'); //Continue your app module
app.controller('Ctrl', function($scope) {
$scope.obj = [
{
intro: "intromessage",
desc: "desc"
},
{
intro: "intromessage2",
desc: "desc"
}
];
});

关于javascript - 当 Controller 在单独的文件中时应用程序不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40419111/

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