gpt4 book ai didi

javascript - 错误(模块 'app' 不可用!)

转载 作者:行者123 更新时间:2023-11-30 14:48:00 25 4
gpt4 key购买 nike

我是 AngularJS 的新手,正在尝试熟悉这个框架。

我收到两个错误(可能是模块未加载的结果):

  • 未能实例化模块应用
  • 模块“app”不可用!

我已经看过了,我没有拼错任何东西。我不知道为什么我不工作。搞了几个小时,应该是个简单的问题。

// guessingGameController.js

var app = angular.module('app');

function GuessingGameController($scope) {
$scope.varifyGuess = function() {
$scope.deviation = $scope.deviation - $scope.guess;
$scope.numOfGuesses = $scope.numOfGuesses + 1;
};

$scope.initializeGame = function() {
$scope.guess = null;
$scope.randNum = Math.floor((Math.random(1000) + 1));
$scope.deviation = null;
$scope.numOfGuesses = null;
};
app.controller('GuessingGame', GuessingGameController);
}

// app.js - create module "app"

var app=angular.module('app', []);
<!doctype html>
<html>

<head>
<script src="../node_modules/angular/angular.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/
3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="app.js"></script>
<script src="js/controllers/guessingGameController.js"></script>
</head>

<body ng-app="app">
<div class="container" ng-controller="GuessingGame">
<h2>Guess the Number !</h2>
<p class="well lead">Guess the computer generated random number between 1 and 1000.</p>
<label>Your Guess: </label><input type="number" ng-model="guess" />
<button ng-click="verifyGuess()" class="btn btn-primary btn-sm">Verify</button>
<button ng-click="initializeGame()" class="btn btn-warning btn-sm">Restart</button>
<p>
<p ng-show="deviation<0" class="alert alert-warning">Your guess is higher.</p>
<p ng-show="deviation>0" class="alert alert-warning">Your guess is lower.</p>
<p ng-show="deviation===0" class="alert alert-success">Yes! That"s it.</p>
</p>
<p class="text-info">No of guesses : <span class="badge">{{noOfTries}}</span>
<p>
</div>

</body>

</html>

最佳答案

有两种使用方式 angular.module ,带有 1 个或 2 个参数。

这引用了一个应该存在的名为“app”的模块,并返回它如果它存在:

var app = angular.module('app');

这将创建一个名为“app”的模块并在成功时返回它:

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

第二个参数(数组),可以包含新模块所依赖的其他模块的列表。

在您的代码中,第一行试图访问一个尚不存在的模块,并且您收到一个错误。

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

function GuessingGameController($scope) {
$scope.numOfGuesses = 0;
$scope.verifyGuess = function() {
console.log($scope.deviation, $scope.guess)
$scope.deviation = $scope.deviation - $scope.guess;
$scope.numOfGuesses = $scope.numOfGuesses + 1;
};

$scope.initializeGame = function() {
$scope.guess = null;
$scope.randNum = Math.floor((Math.random(1000) + 1));
console.log($scope.randNum);
$scope.deviation = null;
$scope.numOfGuesses = null;
};

}

app.controller('GuessingGame', GuessingGameController);
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js"></script>


<body ng-app="app">
<div class="container" ng-controller="GuessingGame">
<h2>Guess the Number !</h2>
<p class="well lead">Guess the computer generated random number between 1 and 1000.</p>
<label>Your Guess: </label><input type="number" ng-model="guess" />
<button ng-click="verifyGuess()" class="btn btn-primary btn-sm">Verify</button>
<button ng-click="initializeGame()" class="btn btn-warning btn-sm">Restart</button>
<div>
<p ng-show="deviation<0" class="alert alert-warning">Your guess is higher.</p>
<p ng-show="deviation>0" class="alert alert-warning">Your guess is lower.</p>
<p ng-show="deviation===0" class="alert alert-success">Yes! That"s it.</p>
</div>
<p class="text-info">No of guesses : <span class="badge">{{numOfGuesses}}</span>
</p>
</div>

</body>

关于javascript - 错误(模块 'app' 不可用!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48599125/

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