Angular Binding与HTML中的Select Box无法使用



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

模板

<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(){
    }
});

设备组中的添加字符串不会出现在选择框中的选项中。怎么了?

select | AngularJS docs ngModel是必需的参数。

通过 ng-model="selected"之类的东西,它会起作用!

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

这是工作示例

请参考此。可能会帮助您尝试使NG选项更简单,并为相同的NG模型添加NG模型 -

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>

尝试这个ng-model =" devicegroups [0]"

最新更新