gpt4 book ai didi

javascript - AngularJS - 设置默认下拉菜单选项时出现问题

转载 作者:行者123 更新时间:2023-11-28 19:10:23 24 4
gpt4 key购买 nike

我是 AngularJS 新手。现在,我在电子商务网站上应用了以下两个过滤器:

                         <div ng-show="user.ShipMethod != null && shippers && orderShipAddress.AddressName != 'BIG'" " ng-class="{'view-form-select': !currentOrder.LineItems[0].ShipperName, '': currentOrder.LineItems[0].ShipperName }">
<label ng-class="{required: !currentOrder.IsMultipleShip() && user.ShipMethod != null}" ng-show="currentOrder.LineItems[0].ShipperName || !currentOrder.IsMultipleShip() && user.ShipMethod != null">{{('Shipping' | r) + ' Method' | xlat}}</label>
<select class="form-control" ng-change="updateShipper()" name="shipMethod"
ng-model="currentOrder.LineItems[0].ShipperName"
ng-show="user.ShipMethod.ShipperSelectionType == 'UserDropDown'"
ng-options="shipper.Name as (shipper.Name + ' ' + (shipper.ShippingRate.Price | currency | xlat)) for shipper in shippers | filter: { Name: '!Pick-Up at BIG'}"
ng-required="!currentOrder.IsMultipleShip() && user.ShipMethod != null" >

<option value=""></option>
</select>
<i class="fa fa-truck"></i>
</div>

<div ng-show="user.ShipMethod != null && shippers && orderShipAddress.AddressName == 'BIG'" ng-class="{'view-form-select': !currentOrder.LineItems[0].ShipperName, '': currentOrder.LineItems[0].ShipperName }">
<label ng-class="{required: !currentOrder.IsMultipleShip() && user.ShipMethod != null}" ng-show="currentOrder.LineItems[0].ShipperName || !currentOrder.IsMultipleShip() && user.ShipMethod != null">{{('Shipping' | r) + ' Method' | xlat}}</label>
<select class="form-control" ng-change="updateShipper()" name="shipMethod"
ng-model="currentOrder.LineItems[0].ShipperName"
ng-show="user.ShipMethod.ShipperSelectionType == 'UserDropDown'"
ng-options="shipper.Name as (shipper.Name + ' ' + (shipper.ShippingRate.Price | currency | xlat)) for shipper in shippers | filter: { Name: 'Pick-Up at BIG'}"
ng-required="!currentOrder.IsMultipleShip() && user.ShipMethod != null" >
<option value=""></option>
</select>
<i class="fa fa-truck"></i>
</div>
</div>

我的 Controller 位于此处:https://big.four51ordercloud.com/test0530/js/directives/ordershipping.js

在我的 Controller 中,我想出了这个来设置默认下拉菜单选项,但我不确定将其放在 updateShipper() 函数中的何处。如果下拉菜单中只有 1 个选项,我希望它为默认值。现在,如果有 1 个选项,用户必须手动选择它,但是,可以填充其他选项,具体取决于是否满足其他过滤器的条件。您看到的第一个过滤器始终会在下拉列表中产生多个选项,而第二个过滤器始终只会产生一个选项 - “Pick-Up at BIG”选项。

$scope.currentOrder.LineItems[0].ShipperName = $scope.shippers[0];

最佳答案

使用 ng-model 设置列表项的默认值。每当您选择一个项目时,您设置的 ng-model 变量都会更新

参见example

<select  ng-options="p for p in animal"
ng-model="selectedanimal"></select>

关于javascript - AngularJS - 设置默认下拉菜单选项时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30821022/

24 4 0