0

Sup guys! I am trying to build a dynamic form, but I got stuck in a ngModel problem.

The idea is:

I got 2 arrays: one for temporary show to user selected info, and another that is which actually is saved on post (I have different types of data, so I have to do this way).

1- The user uses a <select> to choose a option. The selected option is a object inside a list. User hits the add link, and the selected option pop for him.

2- The selected option, has a custom property, that must be set before hit the add button. For that, a ´input´ pop when an option is selected. the ngModel for this is dynamically set, based on a object's property.

3- I need to get the input data, and save it inside a property of the object selected on the list.

4- Push the object to the array-to-be-posted (newData).

What I have that work: the list, the dynamic ngModel. What I need: to get the data from this ngModel ad use it inside my directive.

here go the codes:

html:

        <select id="newPrereq" class="form-control" name="newPrereq" ng-model="newPrereq" ng-options="prereq.name group by prereq.cat for prereq in prereqs | orderBy:prereq.name">
            <option value="" hidden>-- Select --</option>
        </select>
        <input type="{{newPrereq.type}}" ng-show="newPrereq.array" ng-model="newPrereq[ngModel]">

        <a ng-click="addItem(newPrereq, selectedPrereqs, 'prereqs')" ng-show="newPrereq !== undefined || ''">add</a>

        <ul class="list-inline">
            <li ng-repeat="prereq in selectedPrereqs"><span>{{prereq.name}} </span> <a ng-click="removeItem($index, selectedPrereqs, 'prereqs')">[X]</a></li>
        </ul>

directive (scope:false) relevant functions:

            scope.prereqs = Lists.prereqs;

            scope.addItem = function(obj, array, group){
                array.push(obj);                                    // Add item to array-to-be-posted

                if(obj.array){
                    scope.newData.prereqs.proficiencies.push({'name': obj.name, 'cat': obj.cat,  'details': [something here to get the ng-model from DOM]})
                };

                scope[group] = scope[group].filter(function(list){  // Remove added item from the <select>
                    return list !== obj;
                });
                scope.newPrereq = undefined;
            };

part of the list (come from a Lists.js service)

        'prereqs': [
            {'name':'option 1',         'cat':'category 1', 'type':'text',      'ngModel':'smDetails',  'array':true},
            {'name':'option 2',     'cat':'category 2', 'type':'text',      'ngModel':'srDetails',  'array':true},
        ]
  • may be help u http://stackoverflow.com/questions/32470928/angular-formly-adding-form-fields-dynamically-on-user-click/35603088#35603088 – Hadi J Feb 29 '16 at 19:42
  • Not really clear how the directive fits into the view or where the specific problem is in adding this new property. Connecting the data in popup directive and in controller through a service would probably help – charlietfl Feb 29 '16 at 19:49
  • Almost that, @hadiJZ but I still need to access that ngModel, because i am saving the data in 2 arrays at the same time. Anyway, thanks for your time =) – Rafael Eduardo Paulin Feb 29 '16 at 19:50
  • @charlietfl the directive is being used, in this case, for the `scope.addItem` function. I need to save the data from the input into the property `details` of the object I am pushing into the array (if clause of the example)... but i cant get it to work – Rafael Eduardo Paulin Feb 29 '16 at 19:53
  • @charlietfl I need to get the data inserted into this input `` and assign it to the `details` property of the pushed object on the `if` clause – Rafael Eduardo Paulin Feb 29 '16 at 19:57
  • I suggest you make a simple demo of this in [plunker](http://plnkr.co/edit/?p=catalogue). It's very hard to tell from code shown what scopes are what – charlietfl Feb 29 '16 at 20:02
  • @charlietfl here it go: http://plnkr.co/edit/Fkyr2EFwoqPmAQ81lrwV?p=preview – Rafael Eduardo Paulin Feb 29 '16 at 20:33
  • So what is specific problem in demo? Not clear what is expected. Some instructions in demo would help – charlietfl Feb 29 '16 at 20:36
  • @charlietfl nevermind, I found the solution for that.. =D anyway, thank you for your time! – Rafael Eduardo Paulin Feb 29 '16 at 20:47

0 Answers0