gpt4 book ai didi

javascript - Angular .js :14700 Error: [$controller:ctrlreg] The controller with the name 'MainCtrl' is not registered

转载 作者:行者123 更新时间:2023-11-30 11:31:37 35 4
gpt4 key购买 nike

我是 AngularJs 的新手,我正在编写教程。这是我的代码。`

<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<script src="angular.js"></script>
</head>
<body data-ng-init="hourOfDay = 10">
<div data-ng-controller="MainCtrl">
<h1>Angular By Bhaskar </h1>
<p data-ng-if="hourOfDay<12">Good Morning</p>
<p data-ng-if="hourOfDay>17">Good Evening</p>
<p>Hour of the day is:{{hourOfDay}} </p>
</div>
<script>
var module = angular.module('myApp',[]);
module.controller = ("MainCtrl",Main);

function Main(){
console.log("Main function called");
}
</script>

</body>

</html>`

我正在使用 Angular 1.6.6。我在这里做错了什么吗?

最佳答案

它应该声明为,

 module.controller("MainCtrl",Main); 

演示

var module = angular.module('myApp',[]);
function Main(){
console.log("Main function called");
};
module.controller("MainCtrl",Main);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body data-ng-app="myApp" data-ng-controller="MainCtrl" data-ng-init="hourOfDay = 10">
<div >
<h1>Angular By Bhaskar </h1>
<p data-ng-if="hourOfDay<12">Good Morning</p>
<p data-ng-if="hourOfDay>17">Good Evening</p>
<p>Hour of the day is:{{hourOfDay}} </p>
</div>
</body>

关于javascript - Angular .js :14700 Error: [$controller:ctrlreg] The controller with the name 'MainCtrl' is not registered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033287/

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