A
A
Alexander Kovalchuk2015-07-28 17:40:20
Angular
Alexander Kovalchuk, 2015-07-28 17:40:20

How to generate a selection in json and store data for further use using angular?

We have a select field that has a list of names, and depending on the choice of name, it allows you to select details in a separate select field

<div class="form-group">
                <label>Имя</label>
                <select ng-model="user.name" 
                            ng-options=" name.name for name in names">
                </select>
              </div>
              <div class="form-group">
                <label>Режим</label>
                <select ng-model="switches.mode" 
                            ng-options="mode.name as mode.name for mode in user.name.modes">
                </select>
              </div>

Names has the following structure:
{

       "name": "hello",

       "modes": [

          {

              "name": "mode1"

          }

      ]

   }

But you need to create json of this format:
{
             "name": "hello",
             "mode": "mode1"
         }

Can this be done beautifully and without crutches using Angular?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-07-28
Protko @Fesor

all angular can help you with is angular.forEach and angular.copy. as well as map/reduce.

names.map(function (name) {
    // можно и без reduce, просто забрать у первого элемента массива значение, 
    // это просто как пример
    name.mode = name.modes.reduce(function (result, item) { return item.name; });
    delete name.modes;
    return name;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question