单个输入中的ng-repeat值,逗号分隔

时间:2017-12-08 05:02:04

标签: javascript jquery angularjs

我有list array.Inside我有一个名为types的嵌套数组。
我试图在逗号分隔的输入字段中显示types.work

这就是我的尝试:

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
 $scope.init = function(){
 $scope.list=[
   {
     "name":"john",
     "types":[
       {"work":"aa"},
       {"work":"bb"}
     ]
   }
 ];
      }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="MainCtrl" ng-init="init()">
   <input type="text" ng-model="list.name">
   <label ng-repeat="data in list.types">{{data.work}}{{$last ? '' : ', '}}</label>
  </div>
</div>

有谁能告诉我如何在单输入中使用ng-repeat值?。

4 个答案:

答案 0 :(得分:2)

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

app.controller('MainCtrl', function($scope) {
 $scope.init = function(){
 $scope.list=[
   {
     "name":"john",
     "types":[
       {"work":"aa"},
       {"work":"bb"}
     ]
   }
 ];
 
  $scope.input_model_value  = ''
  var  temp = []
  $scope.list.forEach(function(t1){
      t1.types.forEach(function(val){
      temp.push(val.work)
    })
  })
   $scope.input_model_value = temp.join(',')
 }
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="MainCtrl" ng-init="init()">
   <input type="text" ng-model="input_model_value">
   <label ng-repeat="data in list.types">{{data.work}}</label>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用多个 <label ng-repeat="data in list"> <label ng-repeat="d in data.types"> {{d.work}} </label> </label> 来访问嵌套数组。

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
 $scope.init = function(){
 $scope.list=[
   {
     "name":"john",
     "types":[
       {"work":"aa"},
       {"work":"bb"}
     ]
   }
 ];  
 $scope.name = $scope.list[0].types.map((o) => o.work)
  $scope.name =  $scope.name.join(",")
 }
 

});

&#13;
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="MainCtrl" ng-init="init()">
   <input type="text" ng-model="name">
   <label ng-repeat="data in list">
      <label  ng-repeat="d in data.types">
      
        {{d.work}}
      </label>
   
   </label>
  </div>
</div>
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如果您想要其他解决方案。您可以在JS文件中迭代数组并将值分配给输入模型。

&#13;
&#13;
function playMelody()
{
var notes = [49,50,51,52,53,54,55,76,80,45,68,56,87] //more than 1000
var time = [1,1.5,1,2,3,0.5,0.75,0.66,0.78,1.25,1.35,0.75,0.75] //more than 1000, time in seconds for each note value.
var ctxt=MIDI.getContext().currentTime;
var delay = time[0],l=notes.length,j=0;
function play()
{       
         if (j < l) 
         {            
                MIDI.noteOn(0,notes[j],75,ctxt+delay);  
                delay = delay + time[j];    
                j++;                                
         }      

setTimeout(play,time[j]);
}
play(); 
}

playMelody();
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您添加了一个数组列表,因此您可以在ng-repeat中使用ng-repeat,或者您可以直接使用这样的数组第一个对象

-t "SOME CONCAVETOP TEXT" -s outline -e concave-top -d 0.5 -f Arial -p 48 -c skyblue -b white -o black -l 1 -u lightpink