在Angular中,在分配给object属性之前,检查值是否等于数组值之一

时间:2018-04-11 14:49:12

标签: angular typescript

我有类'CheckBoxQuestion'的对象实例。我需要为其属性赋值。有属性调用选项[],这里我正在组装类型清单的键值。现在我的问题是需要比较item.preDefineAnswerOptionId键值,即如果存在于数组中,为此我有单独的函数'contains(a,obj)我调用以获取布尔结果

这个包含函数适用于以下测试代码;

测试代码

for(var item in questionsList[key].answer)
   {
     var dd = this.contains(questionsList[key].answer, questionsList[key].answer[item].answerValue)
   }

但是当我为'optionSelected'属性执行时,它会抛出错误

成分

 else  if(questionElementType=="checkbox")
 {

   let _checkBox = new CheckBoxQuestion ({
     consultationId: questionsList[key].consultationId,
     questionId: questionsList[key].questionId,
     questionElementType: questionsList[key].questionElementType[0].title,           
     questionType: questionsList[key].questionType,
     title:questionsList[key].title,
     displayId: questionsList[key].displayId,
     key: questionsList[key].questionId,     
     value: questionsList[key].answer.length<=0? null : questionsList[key].answer[0].answerValue.toLowerCase(),
     label: questionsList[key].title, 
     order: 7,
     options: questionsList[key].answerOptions.map(function(item){                                                
       return {"name": item.ReferenceKey, "key": item.preDefineAnswerOptionId, "value": item.text, "optionSelected": this.contains(questionsList[key].answer, item.preDefineAnswerOptionId)} // need help here, throw error
    }),
   }); 
     this.mappedQuestions.push(_checkBox);
 }

包含功能

contains(a, obj)
{

let f:string[]=[];

if(a.length>0)
{
  for(var item in a)
  {
    var answerKey = a[item].answerValue.toLowerCase();

    f.push(answerKey)
  } 

  var i = f.length;
  while (i--) {
     if (f[i] === obj.toLowerCase()) {
         return true;
     }
  }
}

return false;

}

错误

 error 1-`formGroup expects a FormGroup instance. Please pass one in.`
 error 2- Cannot read property 'contains' of undefined

3 个答案:

答案 0 :(得分:0)

问题1:

您的form应该是表单组的实例,请使用myForm: FormGroup;

<强>组件:

 import {FormGroup, FormBuilder, Validators} from '@angular/forms';

 myForm: FormGroup;
  constructor(private formBuilder: FormBuilder) {
    this.myForm = this.formBuilder.group({
      title: ['', [Validators.required, Validators.minLength(5)]],
      description: [''],
    });
  }

<强> HTML

 <form [formGroup]="myForm" (ngSubmit)="logForm()">

问题2:

由于您使用的function内部使用this,因此无效。

如果是typescript

,请使用箭头功能
 options: questionsList[key].answerOptions.map((item) => {                                                
       return {"name": item.ReferenceKey, "key": item.preDefineAnswerOptionId, "value": item.text, "optionSelected": this.contains(questionsList[key].answer, item.preDefineAnswerOptionId)} // need help here, throw error
    }),

答案 1 :(得分:0)

你需要改变这个:

function(item) { ... }

使用箭头符号(保留this引用):

(item) => { ... }

答案 2 :(得分:0)

我找到了答案,

options: questionsList[key].answerOptions.map(function(item){                                                
       return {"name": item.ReferenceKey, "key": item.preDefineAnswerOptionId, "value": item.text, "optionSelected": this.contains(questionsList[key].answer, item.preDefineAnswerOptionId)}
}.bind(this)),