ng-repeat with angular form select box

560 views Asked by At

i am working with AngularJS dynamic forms . According to my userid value i generated multiple form field with same model name using ng-repeat. i am using select box for loading location. As per my requirement by default i need to load first option value into that select box. i am using ng-init for loading that first option data but it is not working . inside controller i was trying to get select box model value like $scope.user.location but it shown error. anyone can you please send some js fiddle solution for this problem.

HTML

<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>

                        <select ng-model="user.location" ng-options="v for v in locations" ng-init='user.location=location[0]' name="select2" required>

                        </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>

JS

var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope) {
    $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.adduser = function() {
        var data = $scope.users.map(function(user) {
            //i am trying this method also
            /*$scope.user.location.push($scope.locations[0]);
              $scope.user.location = $scope.user.location.slice();
              console.log($scope.user.location) its doesnt work. */
            return {
                "userid": user.id,
                "manualcomment": user.comment,
                "location": user.location
            }
        });
        console.log("data", data)
    }
});
2

There are 2 answers

0
Julien TASSIN On

One way to do it is to execute your ng-init code in a $timeout callback. Your problem comes from the execution order between your directives. So delay the init with a $timeout to trigger it at the next digest.

Here is an example :

var myApp = angular.module('myApp', []);
myApp.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', 'germany', 'china', 'Dubai'];
  $scope.initLocation = (user) => {
    $timeout(() => {
      user.location = $scope.locations[0];
    });
  }
  $scope.adduser = function() {
    var data = $scope.users.map(function(user) {
      //i am trying this method also
      /*$scope.user.location.push($scope.locations[0]);
        $scope.user.location = $scope.user.location.slice();
        console.log($scope.user.location) its doesnt work. */
      return {
        "userid": user.id,
        "manualcomment": user.comment,
        "location": user.location
      }
    });

    console.log("data", data)
  }

});
<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>

              <select ng-model="user.location" ng-options="v for v in locations" ng-init='initLocation(user)' name="select2" required>

                        </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>

1
Shriyansh Gautam On

You can try

  <md-select ng-model="user.location" name="select2" required>
     <md-option ng-repeat="v in locations" value="{{v}}" ng-selected="v == user.location" >
                                                {{v}}
     </md-option>
  </md-select>

ng-select receives a condition, that option is selected by default for which the condition is true