添加嵌套视图后代码停止工作 - AngularJS
大家好,
我有一个简单的页面,可以根据设置的单词检查文本输入。在我的原始代码中,该页面使用以下内容(original code)
我在代码中添加了嵌套视图,现在我的代码坏了,因为它始终告诉用户文本字段为空(nested view code)
我不确定这是否是我的控制器的问题,但我上下我的代码,我找不到问题。
这是游戏的控制器
gameApp.controller('wordController', function($scope)
{
$scope.guess = '';
$scope.guesses = [];
$scope.guessed= '';
$scope.allowed = 6;
$scope.wordToGuess = "Just";
$scope.guestGuess = "";
$scope.pushGuess = function () {
$scope.guesses.push($scope.guestGuess);
$scope.guessed = $scope.guesses.length;
$scope.resetGuess();
$scope.$apply();
};
$scope.resetGuess = function() {
$scope.guestGuess = '';
};
$scope.addGuess = function()
{
if ($scope.guestGuess === null || $scope.guestGuess === '')
{
$("input[type=text]").ready(function () { $("#guessEntry").addClass("has-error"); });
$("#guessStatus").removeClass("glyphicon glyphicon-thumbs-down form-control-feedback");
$("#guessStatus").addClass("glyphicon glyphicon-remove form-control-feedback");
$scope.result = " Please enter a guess\n\nDon't leave the box empty.";
alert($scope.result);
}
else if ($scope.guestGuess.toLowerCase() == $scope.wordToGuess.toLowerCase())
{
$("input[type=text]").ready(function () { $("#guessEntry").removeClass("has-warning").removeClass("has-error").addClass("has-success has-feedback"); });
$("#guessStatus").removeClass("glyphicon glyphicon-thumbs-down form-control-feedback").removeClass("glyphicon glyphicon-remove form-control-feedback");
$("#guessStatus").addClass("glyphicon glyphicon-thumbs-up form-control-feedback");
$scope.pushGuess(guestGuess);
$scope.result = "You have guessed the correct word. Way to go!\n\n\t\t The word was: ";
alert($scope.result + $scope.wordToGuess);
$scope.resetGuess();
}
else if ($scope.guestGuess != $scope.wordToGuess & ($scope.allowed - $scope.guessed) > 1)
{
$("input[type=text]").ready(function () { $("#guessEntry").removeClass("has-error").addClass("has-warning"); });
$("#guessStatus").removeClass("glyphicon glyphicon-remove form-control-feedback").addClass("glyphicon glyphicon-thumbs-down form-control-feedback");
$scope.pushGuess(guestGuess);
$scope.result = "Please try again!";
alert($scope.result);
}
else if (($scope.allowed - $scope.guessed) <= 1)
{
$("input[type=text]").ready(function () { $("#guessEntry").removeClass("has-warning").addClass("has-error"); });
$("#guessStatus").removeClass("glyphicon glyphicon-thumbs-down form-control-feedback");
$("#guessStatus").addClass("glyphicon glyphicon-remove form-control-feedback");
$scope.guesses.push($scope.guestGuess);
$scope.guessed = $scope.guesses.length;
$scope.result = "Game over! The word was: ";
alert($scope.result + $scope.wordToGuess);
}
$scope.guess = '';
};
});
答案 0 :(得分:1)
似乎是ui-router在加载视图时处理范围创建的方式。我不使用它,所以我不知道它做了什么(我使用并推荐http://angular-route-segment.com/)
所以我把你的wordController
放在game.html
里面并且有用了
http://plnkr.co/edit/8ZSyuNAXiC9JX4vBcwtr
我还建议您使用directives
和factories
而不是controllers
来重新编写您的应用,让factories
控制对猜测的检查,所以你使用factory
和使用GuessService.checkInput($scope.guestGuess)
的api会使您的应用程序更加模块化,更易于调试和测试。
答案 1 :(得分:0)
$scope.addGuess = function()
{
$scope.guestGuess = $("#guessEntry").val();
if ($scope.guestGuess === null || $scope.guestGuess === '')
{
........ //rest of code
我认为缺少对用户输入的引用。