gpt4 book ai didi

javascript - AngularJS 错误 - 无法实例化模块

转载 作者:行者123 更新时间:2023-12-03 10:28:36 26 4
gpt4 key购买 nike

我正在研究 .net book 的 Angular,我正在使用一个带有两个 Controller 的应用程序的基本示例。但是我现在收到此错误,我不明白为什么它无法实例化模块:

http://localhost:53990/Scripts/angular.js 第 4138 行第 9 列出现未处理的异常

0x800a139e - JavaScript runtime error: [$injector:modulerr] Failed to instantiate module myApp due to:

Error: [$injector:nomod] 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.

这是我的代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head>
<meta charset="utf-8" />
<title>AngularJS with .NET</title>
<script src="Scripts/angular.js"></script>
</head>
<body>
<h1>Game setup</h1>
<div ng-controller="ExampleController1">
<h2>Player 1</h2>
<label>Name:</label>
<input type="text" placeholder="Please enter player 1 name" ng-model="name" ng-focus="onNameFocused()" />
<h3 ng-show="name">Player 1 name is {{name}}</h3>
<h3 ng-show="previousName">Previous Player 1 name was {{previousName}}</h3>
</div>

<div ng-controller="ExampleController2">
<h2>Player 2</h2>
<label>Name:</label>
<input type="text" placeholder="Please enter player 2 name" ng-model="name" ng-focus="onNameFocused()" />
<h3 ng-show="name">Player 2 name is {{name}}</h3>
<h3 ng-show="previousName">Previous Player 2 name was {{previousName}}</h3>
</div>

<script>
(function () {
"use strict"
var myAppModule = angular.module('myApp', []);
//myAppModule.filter('customname', function () {
// return function (name) {
// name = name || '';
// var customName = "";
// for (var i = 0; i < name.length; i++) {
// if (name.charAt(i) == "e") {
// customName += "3";
// }
// else if (name.charAt(i) == "o") {
// customName += "0"
// }
// else {
// customName += name.charAt(i);
// }
// }
// return customName;
// }
//})

myAppModule.controller('ExampleController1', ['$scope', function
($scope) { // Explicit dependency injection
$scope.name = "Alex Pop";
$scope.previousName = "";
$scope.onNameFocused = function () {
$scope.previousName = $scope.name;
};
}]);

myAppModule.controller('ExampleController2', ['$scope', function ($scope) {
$scope.name = "Alex Pop";
$scope.previousName = "";
$scope.onNameFocused = function () {
$scope.previousName = $scope.name;
};
}]);
console.log(myAppModule.name);
});

</script>
</body>
</html>

谁能看出它出了什么问题吗?

最佳答案

您将整个 Angular 脚本包装在 IIFE 中,但您从未调用该函数来加载脚本。

(function () {
...
});

您应该解开该函数,或者调用该函数,如下所示:

(function () {
...
}());

关于javascript - AngularJS 错误 - 无法实例化模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29310952/

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