[英]How can I add items to my FormArray in angular?
我正在嘗試向我的 FormArray 添加一些項目,但我總是得到:core.js:6241 ERROR TypeError: Cannot read property 'push' of null。
我可以使用我的get answersFormArray() 將 itens添加到測試數組中,但我無法添加到字母和文本表單數組中。 我嘗試調用letterFormArray.push(value here)但給了我同樣的錯誤。
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 添加到字母和文本FormArray。
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);
}
}
這是我正在創建的 object 的示例...
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;
}
安德烈,你通常有
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 一起,而不是獲取 Letter,另請參閱我們如何使用之前定義的 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(...);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.