大家好,我有一个语言多选输入。在选择语言时,我需要使用动态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>
答案 0 :(得分:2)
试试这个:
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;
答案 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
的小提琴