ng-repeat with angular form选择框

时间:2017-09-11 19:16:56

标签: angularjs angularjs-ng-repeat angularjs-ng-options angularjs-forms angularjs-select

我正在使用AngularJS动态表单。根据我的用户ID值,我使用ng-repeat生成了具有相同型号名称的多个表单字段。我正在使用选择框来加载位置。根据我的要求默认情况下,我需要将第一个选项值加载到该选择框中。我正在使用ng-init加载第一个选项数据,但它无法正常工作。内部控制器我试图得到像$ scope.user.location这样的选择框模型值,但它显示错误。任何人都可以为这个问题发送一些js小提琴解决方案。

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 个答案:

答案 0 :(得分:0)

一种方法是在$ timeout回调中执行ng-init代码。 您的问题来自指令之间的执行顺序。 因此,使用$ timeout延迟init以在下一个摘要中触发它。

以下是一个例子:

&#13;
&#13;
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', 'jermany', '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)
  }

});
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以尝试

  <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接收条件,默认情况下选择该条件为真的