Tech Kid
Tech Kid

Reputation: 577

AngularJS: how to generate dynamic ng-model

My current scenario is: I've doing nesting repetition like follow:

<form name="task_form" ng-app="myApp" ng-submit="tasksubmit()">    
 <ul class="items-list">
      <li ng-repeat="task in taskslist | orderBy:orderProp">
      <p>
        <strong>{{task.title}}</strong>
      </p>
      <select name="task_value" ng-model="task.dropdown_value" class="select-box">
        <option ng-repeat="item in task.dropdown_values" value="{{item}}">{{item}}</option>
      </select>
     </li>
    </ul>
</form>

In <li> I'm repeating the <option> too. I want to generate the dynamic ng-model values in <select ng-model="">.. So that i can get them in controller.. and distinguish them via their task.id.

Upvotes: 0

Views: 415

Answers (1)

Wasimakram Mulla
Wasimakram Mulla

Reputation: 511

Assigning a dynamic value to ng-model is not an Ideal way, what you can do is write a specific name to the ng-model (below)

<select name="task_value" ng-model="dropdown_value" class="select-box" ng-change="checkTaskId(task.id)">

you can even use a ng-change to the select element and traverse through your object using a loop and check the value according to the task.id, but in your controller itself.

Else if you really need the ng-model to be dynamic then below is an easy and alternate solution.

<select name="task_value" ng-model="myData.values[task.dropdown_value]"  class="select-box">

in yout controller you can have it like

$scope.myData= {
    values:{}
};

There are various other ways to do so. ways change according to your requirement. Hope this solves your issue.

Upvotes: 1

Related Questions