我试图将一些项添加到我的FormArray中,但我总是得到:core.js:6241错误类型错误:无法读取null的属性"push"。
我可以使用getanswersFormArray((将itens添加到测试数组中,但无法添加到字母和文本表单数组中。我尝试调用letterFormArray.push(此处为值(,但出现了相同的错误。
this.userForm = this.fb.group({
fname: ['', [Validators.required]],
lname: ['', [Validators.required]],
email: ['', [Validators.email, Validators.required]],
facility: ['', [Validators.required]],
testCode: ['', [Validators.required]],
date: [''],
test: this.fb.group({
name: [''],
id: [''],
position: [''],
scored: [''],
wrong: [''],
duration: [''],
/* answers: this.fb.array([
this.fb.group({
num:'',
letter: this.fb.array([]),
text: this.fb.array([]),
userLetter: this.fb.array([]),
userText: this.fb.array([]),
})
])
}), */
answers: this.fb.group({
user: this.fb.array([
this.fb.group({
num:'',
letter: this.fb.array([]),
text: this.fb.array([]),
})
]),
teste: this.fb.array([
this.fb.group({
num:'',
letter: this.fb.array([]),
text: this.fb.array([]),
})])
})
})
})
我正在尝试将itens添加到letter和textFormArray中。
get answersFormArray() {
//return this.userForm.get('test.answers') as FormArray //Option 1
return this.userForm.get('test.answers.teste') as FormArray
}
get letterFormArray() {
return this.userForm.get('test.answers.teste.letter') as FormArray;
}
get textFormArray() {
return this.userForm.get('test.answers.teste.text') as FormArray;
}
getTestCorrectAnswers() {
for(let i = 0; i < this.current_quest.length; i++) {
const answ = this.fb.group({
numb: i+1,
letter: this.fb.array([]),
text: this.fb.array([])
})
for(let z = 0; z < this.current_quest[i].alternatives.length; z++) {
const alter = this.fb.control("");
//objective
if(this.current_quest[i].type === "objective") {
if(this.current_quest[i].single_or_multi === "single") {
if(this.current_quest[i].alternatives[z].correct == true) {
this.textFormArray.push(this.fb.control(this.current_quest[i].alternatives[z].text));
this.letterFormArray.push(this.fb.control(this.current_quest[i].alternatives[z].letter));
console.log("Objetiva com uma única alternativa --> " + this.current_quest[i].alternatives[z].text);
}
}
else {
if(this.current_quest[i].alternatives[z].correct == true) {
console.log("Objetiva com várias alternativas --> " + this.current_quest[i].alternatives[z].text);
}
}
}
//Subjective
else {
if(this.current_quest[i].alternatives.length >= 1) {
console.log("Subjetiva com chave de resposta --> " + this.current_quest[i].alternatives[z].text);
}
else if(!this.current_quest[i].alternatives.length){
console.log("Subjetiva sem chave de resposta --> ");
}
}
}
this.answersFormArray.push(answ);
}
}
这是我正在创建的对象的一个示例。。。
teste: [
{
"numb": 1,
"letter": ["a", "b"],
"text": ["red", "yellow"]
},
{
"numb": 2,
"letter": ["a"],
"text": ["black"]
}
]
letter
和text
是嵌套在另一个FormArray中的FormArray。由于FormArray更基本上是一个数组,您需要将填充到其中的FromGroup的索引作为目标
get letterFormArray(index: number) {
return this.userForm.get('test.answers.teste[index].letter') as FormArray;
}
André,你通常有
get users()
{
return this.userForm.get('test.answer.users') as FormArray
}
get teste()
{
return this.userForm.get('test.answer.teste') as FormArray
}
要到达FormArray的一个元素,您需要一个";索引";,你不能使用getter,看看这是getLetter在一起,而不是getLetter,还看看我们如何使用之前定义的getter
getLetter(index){
return this.answer.at(index).letter as FormArray
}
getTest(index){
return this.answer.at(index).test as FormArray
}
现在你可以做了
this.getLetter(index.push(...);
//and
this.getTest(index.push(...);