添加嵌套视图后代码停止工作

Code stop working after I added nested views

本文关键字:代码 停止工作 视图 嵌套 添加      更新时间:2023-09-26

添加嵌套视图后代码停止工作 - AngularJS

嗨,伙计们,

我有一个简单的页面,可以根据设置的单词检查文本输入。在我的原始代码中,页面的工作方式如下(原始代码)

  1. 如果用户不输入文本,代码将提示他们不要将文本字段留空。
  2. 比较文本,如果错误,猜测的数量会减少,然后用户将有另一个机会输入正确的答案。
  3. 如果用户得到正确的答案,警报将提示他们正确的答案。
  4. 如果用户用完
  5. 尝试,页面将弹出一个警报,让用户知道他们用完了猜测,并让他们知道正确的单词是什么。
我在代码中添加了嵌套视图,

现在我的代码坏了,因为它总是告诉用户文本字段为空(嵌套视图代码)

我不确定这是否是我的控制器的问题,但我上下浏览了我的代码,但我找不到问题。

这是游戏的控制器

  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 = '';
        };
    });

似乎这是 ui-router 在加载视图时处理范围创建的方式。我不使用它,所以我不知道它的作用(我使用并推荐 http://angular-route-segment.com/)

所以我把你的wordController放在game.html里面,它起作用

http://plnkr.co/edit/8ZSyuNAXiC9JX4vBcwtr

我还恳请您使用比controllersdirectivesfactories来重写您的应用程序,让factories控制对猜测的检查,这样您就会拥有一个带有factory并使用GuessService.checkInput($scope.guestGuess)的 api,这将使您的应用程序更加模块化,更易于调试和测试。

$scope.addGuess = function()
{
    $scope.guestGuess = $("#guessEntry").val();
    if ($scope.guestGuess === null || $scope.guestGuess === '')
        {
        ........ //rest of code

我认为缺少对用户输入的引用。