Angular js oi-select ng-init with ng-repeat

244 views Asked by At

I am working on dynamic forms with ng-repeat. I am using the oi-select library for loading my locations. The oi-select box has multi select feature. on my page loading by default, I am loading first option value in that oi-select box.

But I am using a multi select feature if I select any other option than the first option it gets override with that value. Due to this issue, I have to select again that first option. My question is, how can I load my first option value by default into that oi-select?

Afterwards, if I am select any other options it won't override my first value in select box.http://plnkr.co/edit/m6Q02dlHLBCMVLRLfioh?p=preview

my HTML code

<body class="container row">
      <div ng-app="myApp">
          <div ng-controller="myCtrl">
              <form role="form" name='userForm' novalidate>
                  <div class="container">
                      <div class="row" ng-repeat="user in users">
                          <div class="form-group">
                              <div class="col-md-3">
                                  <label>ID</label>
                                  <input ng-model="user.id" id="user.id" name="user.id" placeholder="Enter bugid" type="text" required readonly disabled>
                              </div>
                              <div class="col-md-3">
                                  <label>Comments</label>
                                  <textarea ng-model="user.comment" id="textarea1" rows="1" required></textarea>
                              </div>
                              <div class="col-md-3 ">
                                  <label>Location</label>
                                  <oi-select ng-model="user.location" multiple oi-options="v for v in locations" ng-init='initLocation(user)' name="select2" required>
                                  </oi-select>
                              </div>
                          </div>

                      </div>
                  </div>
                  <div class="buttonContainer text-center btn-container">
                      <br>
                      <button ng-disabled="userForm.$invalid" type="button" id="adduser" ng-click="adduser()">Add user</button>
                      <button type="button" class="btn button--default btn--small pull-center">Close</button>
                  </div>
              </form>
              <script src="https://code.jquery.com/jquery.min.js"></script>
              <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
              <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
              <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
          </div>
      </div>
  </body>

my js code

var app = angular.module('myApp', ['ngResource', 'oi.select']);
  app.controller('myCtrl', function($scope, $timeout) {
      $scope.ids = [1, 2, 3];
      $scope.users = $scope.ids.map(function(id) {
          return {
              id: id,
              comment: "",
              location: ""
          };
      });
      $scope.locations = ['india', 'usa', 'jermany', 'china', 'Dubai'];
      $scope.initLocation = (user) => {
          $timeout(() => {
              user.location = $scope.locations[0];
          });
      }
      $scope.adduser = function() {
          var data = $scope.users.map(function(user) {
              return {
                  "userid": user.id,
                  "manualcomment": user.comment,
                  "location": user.location
              }
          });

          console.log("data", data)
      }

});
0

There are 0 answers