Javascript 在更改数组位置和以角度动态显示内容时遇到问题

Javascript 在更改数组位置和以角度动态显示内容时遇到问题,javascript,angularjs,Javascript,Angularjs,我有一个对象数组,每个对象都包含一个字符串数组。我使用ng repeat来列出所有字符串,并且有一个按钮可以更改为下一组字符串(通过更改最外层数组中选定的对象)。这是我的密码 HTML 数组中的每个对象看起来都像这样 { question: "question A", choices: ["choice 1", "choice 2", "choice 3"], correct answer: 0 } 我做了检查,以确保计数是更新的,它是。如果计数正在更新,视图不应该随之更新吗?我也尝试过

我有一个对象数组,每个对象都包含一个字符串数组。我使用ng repeat来列出所有字符串,并且有一个按钮可以更改为下一组字符串(通过更改最外层数组中选定的对象)。这是我的密码

HTML

数组中的每个对象看起来都像这样

{
 question: "question A",
 choices: ["choice 1", "choice 2", "choice 3"],
 correct answer: 0
}
我做了检查,以确保计数是更新的,它是。如果计数正在更新,视图不应该随之更新吗?我也尝试过这个功能,但仍然无法让它工作

$scope.upCount = function(){
    $scope.count = $scope.count + 1;
    $scope.question = allQuestions[$scope.count].question;
    $scope.choices = allQuestions[$scope.count].choices;
};

感谢您的帮助

您只需在范围内设置所有问题。我认为你不需要两个单独的模块来跟踪当前的问题count仅此一项就可以达到目的

var myApp = angular.module('myApp', []);
function questionCtrl($scope){
    $scope.allQuestions = [....];
    $scope.count = 0;
    $scope.upCount = function(){
        /* make sure index don't run out of range */
        if($scope.allQuestions[$scope.count + 1])
            $scope.count = $scope.count + 1;
    };
    $scope.upCount = function(){
        /* make sure index don't run out of range */
        if($scope.allQuestions[$scope.count - 1])
            $scope.count = $scope.count - 1;
    };
}    
然后在HTML中,您可以简单地执行以下操作:

<body ng-app="myApp">
    <div ng-controller="questionCtrl">
        <form>
            <h1>{{allQuestions[count].question}}</h1>
            <ul>
                <li ng-repeat="choice in allQuestions[count].choices">
                    <input type="radio" name = "answer">{{choice}}</li>
                <li>{{count}}</li>
            </ul>
            <button ng-click= "upCount()">Submit Form</button>
            <button id='goBack'>Go Back</button>
        </form>
    </div>
</body>

{{allQuestions[count].question}
  • {{choice}}
  • {{count}}
提交表格 回去
对我有效,因此在函数中再次设置属性将使其有效。我不知道为什么它以前不起作用。谢谢你的帮助。所以我试过了,但似乎不起作用。执行此操作时,数组中没有任何字符串显示。您可以在ng REPLATE指令中添加其他变量(计数)吗?我所有的数据都在同一个模块中,虽然我可能忽略了一种更简洁的编码方式,但我不明白它为什么不起作用。谢谢你的建议。是的,你可以在ng repeat中有一个范围变量。这是完全正确的。我建议做:$scope.allQuestions而不是var-allQuestions。
var myApp = angular.module('myApp', []);
function questionCtrl($scope){
    $scope.allQuestions = [....];
    $scope.count = 0;
    $scope.upCount = function(){
        /* make sure index don't run out of range */
        if($scope.allQuestions[$scope.count + 1])
            $scope.count = $scope.count + 1;
    };
    $scope.upCount = function(){
        /* make sure index don't run out of range */
        if($scope.allQuestions[$scope.count - 1])
            $scope.count = $scope.count - 1;
    };
}    
<body ng-app="myApp">
    <div ng-controller="questionCtrl">
        <form>
            <h1>{{allQuestions[count].question}}</h1>
            <ul>
                <li ng-repeat="choice in allQuestions[count].choices">
                    <input type="radio" name = "answer">{{choice}}</li>
                <li>{{count}}</li>
            </ul>
            <button ng-click= "upCount()">Submit Form</button>
            <button id='goBack'>Go Back</button>
        </form>
    </div>
</body>