Angular FormArray Insert

时间:2017-09-27 12:06:27

标签: angular typescript angular-forms angular4-forms

我正在尝试使用insert()更新formArray上的特定值。 我在ngOninit()上初始化表格如下:

this.myForm = this.fb.group({
    exercises: this.fb.array([]),
    type: new FormControl(),
    day: new FormControl(),
    sets: this.fb.array([]),
    reps: this.fb.array([]),
});

我有一个输入,在更改时我调用下面的函数但是当我尝试使用数组的索引插入一个新值时,我得到了推送值。

onSetsChange(sets, index){
  var setsFormArray = <FormArray>this.myForm.controls.sets;
  setsFormArray.insert(index, new FormControl(sets));
}

html代码如下:

<div class="col-md-3">
  <select class="form-control input-sm" (change)="onSetsChange($event.target.value, exerciseIndex)">
     <option *ngFor="let set of sets; let i = index;">{{set}}</option>
  </select>
</div>

我传递的 exerciseIndex 来自一个它没有显示的循环。

我做错了什么,它没有更新的价值?谢谢

2 个答案:

答案 0 :(得分:0)

尝试使用patchValue: -

public ngOnInit() {
    const arr = new FormArray([
                new FormControl(),
                new FormControl()
            ]);

    let control = arr.controls[0];
    setTimeout(() => control.patchValue("new-val"), 250); // Set value to "new-val"
    setTimeout(() => console.log(control.value), 350); // Logs "new-val"
}

答案 1 :(得分:0)

public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "realEstateQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA = "opta"; //option a
private static final String KEY_OPTB = "optb"; //option b
private static final String KEY_OPTC = "optc"; //option c
private static final String KEY_OPTD = "optd"; //option d

private SQLiteDatabase dbase;

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    dbase = db;
    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
            + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
            + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT," + KEY_OPTD + " TEXT)";
    db.execSQL(sql);
    addQuestions(db);
    //db.close();
}

private void addQuestions(SQLiteDatabase db) {
    Question q1 = new Question("How many houses can I buy?",
            "1", "2", "3", "4",
            "A");
    this.addQuestion(q1, db);
    Question q2 = new Question("Which of the following is NOT an option ",
            "Apartments", "Houses", "Duplexes", "Rentals",
            "D");
    this.addQuestion(q2, db);
    Question q3 = new Question("Which of the following is the fastest way of buying",
            "Credit card", "Loan", "Cash", "Handshakes",
            "C");
    this.addQuestion(q3, db);
    Question q4 = new Question("Should you use an agent or the internet",
            "Agent", "Internet", "Neither", "Both",
            "A");
    this.addQuestion(q4, db);
    Question q5 = new Question("Which of the following is not an available language",
            "English", "Italian", "Spanish", "Hungarian",
            "C");
    this.addQuestion(q5, db);
    Question q6 = new Question("Question 6",
            "Answer 1", "Answer 2 - correct", "Answer 3", "Answer 4",
            "B");
    this.addQuestion(q6, db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
    onCreate(db);
}

// Adding new question
public void addQuestion(Question quest, SQLiteDatabase db) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_QUES, quest.getQuestion());
    values.put(KEY_ANSWER, quest.getAnswer());
    values.put(KEY_OPTA, quest.getOptionA());
    values.put(KEY_OPTB, quest.getOptionB());
    values.put(KEY_OPTC, quest.getOptionC());
    values.put(KEY_OPTD, quest.getOptionD());

    // Inserting Row
       db.insert(TABLE_QUEST, null, values);
}

这是我想出的第二个答案。谢谢