我是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>
我的控制器位于:https://big.four51ordercloud.com/test0530/js/directives/ordershipping.js
在我的控制器中,我想出了这个来设置默认的下拉菜单选项,但我不确定将它放在updateShipper()函数中的哪个位置。如果下拉菜单中只有1个选项,我希望它为默认值。现在,如果有1个选项,用户必须手动选择它,但是,可以填充其他选项,具体取决于是否满足其他过滤器的条件。您看到的第一个过滤器总是会在下拉列表中产生多个选项,而第二个过滤器总是会产生一个选项 - “BIG上的拾取”选项。
$scope.currentOrder.LineItems[0].ShipperName = $scope.shippers[0];
答案 0 :(得分:3)
使用ng-model设置列表项的默认值。无论何时选择项目,您设置的ng-model变量都将添加
请参阅example
<select ng-options="p for p in animal"
ng-model="selectedanimal"></select>