gpt4 book ai didi

javascript - AngularJS Controller 内的数组未初始化

转载 作者:行者123 更新时间:2023-11-27 22:47:47 26 4
gpt4 key购买 nike

我正在编写一个小型对话框 Controller ,它接受一个问题、一组答案并标记正确答案的索引。我需要使用户能够添加多个问题,并将它们存储在一个数组中。

一切正常,直到我尝试将包含问题详细信息的 JSON 推送到我之前创建的数组中。尝试推送时,JS 会抛出错误:无法读取未定义的属性“push”。我读过一些关于如何在 Angular 中将元素/值推送到数组的线程,但似乎没有人遇到完全相同的问题。

这是我的代码:

function TestController($mdDialog, $scope, $mdSidenav, $mdBottomSheet, $timeout, $log) {
$scope.questionSet = [];
$scope.question = {
text: null,
answers: [],
correctIndex: -1
};

$scope.clickPoP = function(ev) {
// Appending dialog to document.body to cover sidenav in docs app
// Modal dialogs should fully cover application
// to prevent interaction outside of dialog
var parentEl = angular.element(document.body);

$mdDialog.show({
parent: parentEl,
targetEvent: ev,
templateUrl: "edit-word-dialog.tmpl.html",
locals: {
items: $scope.question
},
controller: DialogController
});

function DialogController($scope, $mdDialog) {
$scope.cancel = function() {
$mdDialog.hide();
}

$scope.addQuestion = function(nextQuestion) {
if (nextQuestion === true) {
console.log($scope.question);
$scope.questionSet.push($scope.question);
console.log('added question - clearing object - waiting for next input');
} else {
console.log($scope.questionSet);
console.log('added question - closing dialog box');
$mdDialog.hide();
}
}

$scope.setAnswer = function(index) {
$scope.question.correctIndex = index;
console.log(index);
}
}
};
}

如您所见,questionSet 应该保存问题,并在 Controller 启动时定义和初始化。

然后在 addQuestion() 函数中,我尝试将内部包含所有正确数据的 JSON 对象推送到 questionSet 中,这就是错误所在抛出。

我已经检查了所有数据、所有数据绑定(bind),一切都是正确的,只是不会将 JSON 推送到数组中。请注意,我宁愿避免附加到数组,以使其更易于管理。另请注意,我对 Angular 还很陌生。

感谢您的所有帮助,并对冗长的阅读表示歉意。

这是对话框的代码,以防错误出现:

<md-dialog class="email-dialog" flex="80" flex-sm="100">
<md-toolbar class="toolbar-default" md-theme="{{triSkin.elements.toolbar}}" palette-background="myPurple:500">
<div class="md-toolbar-tools">
<h2>
<span>Quiz</span>
</h2>
<span flex></span>
</div>
</md-toolbar>
<md-divider></md-divider>
<md-dialog-content class="sticky-container">
<div>
<md-content>
<md-input-container>
<label for="question-text">Enter Question</label>
<input type="text" id="quiz-question" label="quiz-question" name="quiz-question" ng-model="question.text">
</md-input-container>
<div style="float: right; width: 10%">
<md-radio-group>
<md-radio-button value="0" aria-label="ans0" ng-click="setAnswer(0)"></md-radio-button>
<br/>
<br/>
<md-radio-button value="1" aria-label="ans1" ng-click="setAnswer(1)"></md-radio-button>
<br/>
<br/>
<md-radio-button value="2" aria-label="ans2" ng-click="setAnswer(2)"></md-radio-button>
<br/>
<br/>
<md-radio-button value="3" aria-label="ans3" ng-click="setAnswer(3)"></md-radio-button>
</md-radio-group>
</div>
<div style="float: right; width: 80%;">
<md-input-container>
<label for="answer-one">Enter answer</label>
<input type="text" id="quiz-answer-one" label="quiz-answer-one" name="quiz-answer-one" ng-model="question.answers[0]">
</md-input-container>
<md-input-container>
<label for="answer-two">Enter answer</label>
<input type="text" id="quiz-answer-two" label="quiz-answer-two" name="quiz-answer-two" ng-model="question.answers[1]">
</md-input-container>
<md-input-container>
<label for="answer-three">Enter answer</label>
<input type="text" id="quiz-answer-three" label="quiz-answer-three" name="quiz-answer-three" ng-model="question.answers[2]">
</md-input-container>
<md-input-container>
<label for="answer-four">Enter answer</label>
<input type="text" id="quiz-answer-four" label="quiz-answer-four" name="quiz-answer-four" ng-model="question.answers[3]">
</md-input-container>
</div>
<div style="float:left;">
<md-button class="md-primary" aria-label="AddQuestion" ng-click="addQuestion(true)">Add Question</md-button>
&nbsp;
<md-button class="md-secondary" aria-label="Save" ng-click="addQuestion(false)">Save</md-button>
</div>
</md-content>
</div>
</md-dialog-content>
<div class="md-actions" layout="row">
<span flex></span>
<md-button class="md-primary" aria-label="Close" ng-click="cancel()">
Close
</md-button>
</div>

最佳答案

您在父 Controller 中定义 $scope.questionSet,而不是在对话框 Controller 中。

MdDialog 默认使用隔离范围。您可以通过在 TestController 中的 $mdDialog 调用中传递 range: $scope 来使用父 Controller 作用域。

关于javascript - AngularJS Controller 内的数组未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38293805/

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