angularjs ng-selected不工作

ogq8wdun  于 2023-10-15  发布在  Angular
关注(0)|答案(4)|浏览(123)

我的问题是ng-selected被设置为true,但该选项未被选中
这是我的控制器代码

.controller("PendingInvoiceCtrl", function($scope, $location, safeApply, dataService) {
    var userData = dataService.data();
    var mauth_token = userData.mauthToken;
    var mauth_acntId = userData.thisApartment.account;
    var apt_id = userData.thisApartment.id;

    $scope.house_list = userData.thisApartment.house;
    $scope.selectedHouseId = $location.search().houseId;

    console.log($scope.selectedHouseId);
});

这是我的HTML代码

<select ng-model="selectedHouseId">
      <option ng-repeat="house in house_list" ng-selected="{{ house.house_id == selectedHouseId }}" value="{{ house.house_id }}">
           {{ house.house_display_name }}
      </option>
 </select>

下面是我的数据格式

{
      house:[0]:{
           house_display_name: "paras, 101",
           house_id: "520755"
      }
 }
gorkyyrv

gorkyyrv1#

ng-属性不需要额外的花括号。试试看:

<option ng-repeat="house in house_list" ng-selected="house.house_id == selectedHouseId" ng-value="house.house_id">
    {{house.house_display_name}}
</option>

更好的方法是使用select标记的ng-options可能性。这将导致:

<select
    ng-model="selectedHouseId"
    ng-options="house.house_id as house.house_display_name for house in house_list">
</select>

然后,您不需要手动担心选项的selected属性,因为它将根据模型的值选择一个。

0s0u357o

0s0u357o2#

您的选项标签应该是:

<option ng-repeat="house in house_list" ng-selected="house.house_id == selectedHouseId" ng-value="house.house_id">
0qx6xfy6

0qx6xfy63#

如果你一起使用ng-Selected和ng-Model,这是从来没有真正指定过的。看起来在某些时候ng-Model优先于ng-Selected。

mjqavswn

mjqavswn4#

我建议你使用Angular Directive ng-options。

<select ng-options="house.house_id as house.house_display_name for house in house_list" ng-model="selectedHouseId"></select>

相关柱塞:http://plnkr.co/8LGz5o0d2gDRoRHYr4pQ

相关问题