Angular Binding不能与HTML中的Select Box一起使用

时间:2017-05-05 05:42:34

标签: javascript angularjs

我准备了一个简单的模板,显示一个带选项的选择框。

模板

<div class="jumbotron" style="background-color:white">
</div>
<div class="jumbotron container-fluid">
<h3 align="center">PAN Bulk Upload</h3>
</div>
<div class="container">
<div class="row">
<div class="col-lg-9">
<div style="border-right:1px solid #cbc6c6">
<div class="container panel-body">
    <label class="custom-file-upload">
        <input id="fileChoose" type="file" custom-on-change="uploadFile" />
        <i class="fa fa-cloud-upload"> Choose Device Group File</i>
    </label>
    <hr/>
    <select size=5 style="width:200px;height:100px" ng-options="o as o for o in deviceGroups">
    </select>
</div>
<div class="container">
    <button ng-click="validateDeviceGroups()">Validate</button>
    <button ng-click="commitDeviceGroups()">Commit</button>
</div>
</div>
</div>
<div class="col-lg-3">
<textarea rows="20" cols="35"></textarea>
</div>
</div>
</div>

角度控制器

angapp.controller('panbulkCtrl', function($scope) {

    $scope.deviceGroups = ["Hi","Hello"];
    $scope.uploadFile = function() {
        var filename = event.target.files[0].name;
        var reader = new FileReader();
        reader.onload = function (e) {
            var rows = e.target.result.split("\n");
            for (var i = 0; i < rows.length; i++) {
                var cells = rows[i].split(",");
                for (var j = 0; j < cells.length; j++) {
                    console.log(cells[i]);
                    $scope.deviceGroups.push(cells[i]);
                }              
            }           
        }
        reader.readAsText(event.target.files[0]);

    };

    $scope.validateDeviceGroups = function(){

    }
});

deviceGroups中添加的字符串不会显示在选择框中作为选项。有什么问题?

2 个答案:

答案 0 :(得分:3)

根据select | AngularJS docsngModel是必需的参数。

ng-model="selected"之类的那样传递它,它会起作用!

<select size=5 style="width:200px;height:100px" ng-model="selected" 
        ng-options="o as o for o in deviceGroups">
</select>

这是working example

答案 1 :(得分:0)

请参考这个。可以帮助您尝试简化ng-options并为其添加ng-model -

var myApp = angular.module("myApp",[]);
myApp.controller("myCntr",function($scope){
$scope.deviceGroups = ["hi","hello"];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCntr">
 <select multiple size=5 style="width:200px;height:100px" ng-options="o for o in deviceGroups" ng-model="selected"/>
</div>