AngularJS选择框

时间:2016-08-15 15:05:52

标签: angularjs

我在angularjs的选择框中遇到问题

我使用这个获取我的locationList:

<select ng-options="location.strBarangay for location in locationList"
                                        ng-model="details.location" id="crOrderLoc">
                                </select>

结果如下:Result

这就是我将它添加到视图中的选择框的方式:

$scope.details.location = {
        dblLocationPrice: $scope.locationList[0].dblLocationPrice,
        intLocationID: $scope.locationList[0].intLocationID,
        intLocationStatus: $scope.locationList[0].intLocationStatus,
        strBarangay: $scope.locationList[0].strBarangay,
        strCity: $scope.locationList[0].strCity
    };

我的问题是,每当我选择一个选项时,总会有一个空白选项,它的值是我json中的第一个对象。 示例:我有一个json对象[{1},{2},{3}] 在我的选择中它将安排如下:

  1. 选项值= 1(它将显示为空)
  2. 选项值= 2(它将显示1)
  3. 选项值= 3(将显示2)
  4. 我试过这个在select

    中设置我的模型的值
    try_files

    但是有一个错误: angular.min.js:117 TypeError:无法读取未定义的属性“0”     在新的prodSalesCtrl

    但它在这里工作http://jsfiddle.net/MTfRD/3/(已编辑)

    但它不适用于我的选择

1 个答案:

答案 0 :(得分:1)

加载数据后需要设置select的默认值:

locationFactory.getLocation().then(function (data) {
    $scope.locationList = data.data.locationList;
    if($scope.locationList && $scope.locationList.length>0)
        $scope.details.location = $scope.locationList[0];
});
相关问题