Set initially selected item in Select list in Angular2

后端 未结 7 1159
独厮守ぢ
独厮守ぢ 2020-12-07 23:59

I\'ve managed to get a Select list to bind with my model for the purpose of saving, but I cannot work out how to make Angular2 automatically select the correct option on the

7条回答
  •  萌比男神i
    2020-12-08 00:43

    I hope it will help someone ! (works on Angular 6)

    I had to add lots of select/options dynamically and following worked for me:

    ( here
    is used to add specific input forms )

    and in *.ts

    // initial state of the model
    // q_options in html = NewOption and its second argument is option type
    model = new NewQuestion(1, null, 2, 
      [
        new NewOption(0, 'text', '', 1), 
        new NewOption(1, 'imagelocal', '', 1)
      ]);
    
    // dropdown options
    objects = [
        {type: 'text', name: 'text'},
        {type: 'imagelocal', name: 'image - local file'},
        {type: 'imageurl', name: 'image URL'}
       ( and etc.)
    ];
    

    When user adds one more 'input option' (pls do not confuse 'input option' with select/options - select/options are static here) specific select/option, selected by the user earlier, is preserved on each/all dynamically added 'input option's select/options.

提交回复
热议问题