如何解决如何以角度向我的 FormArray 添加项目?
我正在尝试向我的 FormArray 添加一些项目,但我总是得到: core.js:6241 错误类型错误:无法读取 null 的属性“push”。
我可以使用我的 get answersFormArray() 将 itens 添加到测试数组,但我无法添加到 letter 和 text > 形成数组。 我尝试调用 letterFormArray.push(value here) 但给了我同样的错误。
this.userForm = this.fb.group({
fname: ['',[Validators.required]],lname: ['',email: ['',[Validators.email,Validators.required]],facility: ['',testCode: ['',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:'',})
]),teste: this.fb.array([
this.fb.group({
num:'',})])
})
})
})
我正在尝试将 itens 添加到 letter 和 text 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,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;
}
,
安德烈,你通常有
myint
要到达 FormArray 的元素,您需要一个“索引”,并且不能使用 getter,请参阅将 getLetter 放在一起,而不是 get Letter,另请参阅我们如何使用之前定义的 getter
get users()
{
return this.userForm.get('test.answer.users') as FormArray
}
get teste()
{
return this.userForm.get('test.answer.teste') as FormArray
}
现在可以了
getLetter(index){
return this.answer.at(index).letter as FormArray
}
getTest(index){
return this.answer.at(index).test as FormArray
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。