简体   繁体   中英

How to set array objects int json object with AngularJS?

i want load array objects from multi select control, then i want load model object called "name" with his name and age values, then i want load array from select and load in model object.... but the ng-model from select control not work :/

<input type="text" class="form-control" ng-model="model.name" placeholder="Put your name..." />
<input type="text" class="form-control" ng-model="model.age" placeholder="Put your age..." />

<!--Select pets for model person-->
<select ng-repeat="pets in arrayFromApi" class="selectpicker" multiple>
   <option id="{{pet.id}}" ng-model="model.pets.id">{{pet.name}}</option>
</select>


<script>
   var app = angular.module("myApp", []);
   app.controller("myCtrl", function($scope) {

    $scope.model = { "name":"", "age":"", "pets" :[ {"id":""} ] };

    $scope.arrayFromApi = function() {
          ......
        this function get id names
     }
 });
 </script>

You will probably need to use ng-options. You can give it a try with ng-repeat but then it should be on option tag only.

Avoid using ng-repeat on the dropdowns, it casuses some performance issues. instead use ng-options.

And for havint the model with name, age and pets. check the mixData funciton.

    <input type="text" class="form-control" ng-model="model.name" placeholder="Put your name..." />
    <input type="text" class="form-control" ng-model="model.age" placeholder="Put your age..." />

    <!--Avoid ng-repeat in dropdowns-->
    <!--Select pets for model person-->
    <select ng-change="mixData()" class="selectpicker" multiple ng-model="myPets" ng-options="pet.id as pet.name for pet in arrayFromApi">
       <option value="">Select...</option>
    </select>

    <!--<select ng-repeat="pets in arrayFromApi" class="selectpicker" multiple>
       <option id="{{pet.id}}" ng-model="model.pets.id">{{pet.name}}</option>
    </select>-->


    <script>
       var app = angular.module("myApp", []);
       app.controller("myCtrl", function($scope) {

        $scope.model = { "name":"", "age":"", "pets" :[ {"id":""} ] };

        $scope.arrayFromApi = function() {
              ......
            this function get id names
         }

        $scope.mixData = function(){
           $scope.model.pets = $scope.myPets;
        };
     });
     </script>

It's much better if you use select as angular standards ng-options with this attribute you can handle everything in your view and multiple type return array to your controller as {id: n}

 var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) { $scope.model = {}; //from api $scope.arrayFromApi = [{ id: 1, name: 'test' }, { id: 2, name: 'test2' } ]; $scope.getDetails = function() { console.log($scope.model); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <input type="text" ng-model="model.name" placeholder="Put your name..." /> <input type="text" ng-model="model.age" placeholder="Put your age..." /> <!--Select pets for model person--> <select ng-model="model.pets" ng-options="{id: item.id} as item.name for item in arrayFromApi" multiple></select> <button ng-click="getDetails()">save</button> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM