默认值不是?对象:null?在选择选项Angularjs中

时间:2018-01-17 04:14:33

标签: javascript html angularjs html5

这是文件js中的代码

$scope.ListOption = [];
$scope.ListOption.push({ Value: "0", Name: Car });
$scope.ListOption.push({ Value: "1", Name: House });

以下是代码HTML的样子

<select class="form-control" id="Category" ng-model="Category">

<option ng-repeat="option in ListOption" value="{{option.Value}}">
 {{option.Name}}</option>

</select>

生成的html是

<select class="form-control ng-pristine ng-valid" ng-model="Category" style="padding: 0px;">
<option value="? object:null ?"></option>
<option ng-repeat="option in ListOption" value="0" class="ng-binding ng-scope">Car</option>
<option ng-repeat="option in ListOption" value="1" class="ng-binding ng-scope">House</option>
</select>

我在这个问题上非常头疼。期待有人帮助我。

3 个答案:

答案 0 :(得分:1)

这是错的。您正在与option匹配。更改。我建议,使用track by $index进行无重复选项

<option value="{{option.Value}}" ng-repeat="option in ListOption track by $index">
 {{option.Name}}
</option>

答案 1 :(得分:1)

我认为错误在于你使用main数组来获取值而不是iterable选项对象。实际上,这应该有效:

<select class="form-control" id="Category" ng-model="Category">
<option ng-repeat="option in ListOption" value="{{option.Value}}">
 {{option.Name}}
</option>
</select>

答案 2 :(得分:1)

希望这是有帮助的:)“

VIEW

<select class="form-control" data-ng-options="list.Name for list in ListOption" data-ng-model="selectedValue">

MODEL

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http, $log) {
$scope.ListOption = [];
$scope.ListOption.push({ Value: "0", Name: "Car" });
$scope.ListOption.push({ Value: "1", Name: "House" });
$scope.selectedValue = [];

$scope.selectedValue = $scope.ListOption[0];

});