将列表播放器名称显示为选择选项视图表单

时间:2017-03-06 18:53:32

标签: angularjs

因为我可以在附图中看到的选项中只显示视图中玩家姓名的列表。在html5表单中如何显示名称列表?

// players.js

 var db = firebase.database().ref('players/');
  db.on('value', function(snapshot){
    var players = snapshot.val();
    var player = '';
    for (player in players) {

         console.log(players[player].name); //show list name of players

    }
  }, function(errorObject) {
    console.log("error");
  })

see image

谢谢!

2 个答案:

答案 0 :(得分:1)

感谢A2H,我的代码有效:

    $scope.players = [];
     var db = firebase.database().ref('players/');
     db.on('value', function(snapshot){
     var players = snapshot.val();
     var player = '';
     for (player in players) {

     $scope.players.push({name:players[player].name})
   }
  }, function(errorObject) {
    console.log("error");
  })

HTML5:

    <label>Slected player: {{selected}}</label><br>
    <select ng-options="p as p.name for p in players" ng-model="selected">

    </select>

答案 1 :(得分:0)

看看这个JS Fiddle example

<select ng-options="p as p.name for p in players" ng-model="selected">

ng-options="p as p.name for p in players"会填充您的下拉列表,ng-model="selected"会定义一个名为selected的模型,其中包含所选的player对象。