使用多个下拉和ng-reapeat的动态ng模型

时间:2017-02-08 10:15:42

标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

大家好,我有一个语言多选输入。在选择语言时,我需要使用动态ng-model打开所选语言的文本框。  这是我的json和代码。

[{"id":"1","lang":"English"},{"id":"2","lang":"Arabic"},{"id":"3","lang":"Français"}]

<select class="form-control" ng-model="language" multiple required >
   <option value="" disabled="">Select Language</option>
   <option ng-repeat="l in lang" value="{{l.id}}">{{l.lang}}</option>
</select>

<div class="form-group" ng-repeat="lng in language" >
   <label class="col-md-2" style="padding-right: 0px;">Name {{lang[lng-1].lang}}</label>
   <div class="col-md-10" >

       <input type="text" ng-model="typename.lang[lng-1].id" required >
   </div>

2 个答案:

答案 0 :(得分:2)

试试这个:

&#13;
&#13;
var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
    $scope.lang = [{
	"id": "1",
	"lang": "English"
}, {
	"id": "2",
	"lang": "Arabic"
}, {
	"id": "3",
	"lang": "Français"
}];

$scope.submitLang = function(selectedLang) {
 console.log(selectedLang);
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <select class="form-control" ng-model="language" ng-options="option.lang for option in lang" multiple required ></select>
  <div ng-repeat="lng in language">
   <label>{{lng.lang}}</label>
   <input type="text" ng-model="selectedLang" required ng-change="submitLang(selectedLang)">
  </div>
  </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

就我个人而言,我会使用ng-options而不是像这样的ng-repeat

控制器:

$scope.inputs = [];
    $scope.languagues = [{"id":"1","lang":"English"},{"id":"2","lang":"German"},{"id":"3","lang":"Spanish"}];
    $scope.showTextbox= function (option) {
       $scope.inputs = [];
       for(var i = 0; i < option.length; i++){
          $scope.inputs.push({name: option[i], value: 'test'+i})
       }  
    }      

HTML:

<select class="form-control" ng-model="language" ng-options="option.lang as option.lang for option in languagues"   ng-change="showTextbox(language)" multiple required ></select>
  <div ng-repeat="item in inputs">
  <br>
    <label>{{item.name}}</label><br>
    <input type="text" ng-model="item.value" required >
    <p>
       {{item.value}}
    </p>
  </div>   

这是一个显示变化Fiddle

的小提琴