简体   繁体   English

Angular JS选择ng-options而不是ng-repeat

[英]Angular js select ng-options instead of ng-repeat

I have a select box where i generated options using ng-repeat, but i want that to be generated using ng-options 我有一个选择框,其中我使用ng-repeat生成了选项,但我希望使用ng-options生成该选项。

<select ng-change="onChangeShippingMethod(shippingMethodId)" ng-model="shippingMethodId" class="input-full-w normal_dropDown" name="gjh" id="select_shipping_method" >
    <option ng-repeat="shippingDetail in shipModeArr" value="{{shippingDetail.shipModeId}}" ng-selected="shippingDetail.shipModeId == selectedAddressPerItem[cartItems[0].cartItemId].shipModeId">
        {{shippingDetail.description}}&nbsp;Arrives {{shippingDetail.estArrivalDate | changeDateFormat | date:'mediumDate'}}{{shippingDetail.shipCharge | currency}}
    </option>
</select>

and i want a particular options should be selected based on some condition. 我希望根据某些条件选择特定的选项。

and using ng-repeat an empty options is coming with the other options 并且使用ng-repeat一个空选项与其他选项一起出现

Please check in fiddle 请检查小提琴

It would be great if you would have provided some data then somebody would have developed fiddle for it Now i just can give you syntax try this applying on your code. 如果您将提供一些数据,然后有人会为此提琴,那就太好了。现在我可以给您语法,尝试在您的代码上应用此方法。

<select class="form-control" ng-model="searchState" ng-options="st.state for st in stateList | filter:searchCountry | orderBy:'state'">
<option value="">Select State</option>

Here i have demo in jsfiddle example for the ng-options. 在这里,我在jsfiddle示例中有ng-option的演示。 fiddle for particular conditions to satisfy please mention those in next question or use filters as i showed in above code. 为满足特定条件而摆弄 ,请在下一个问题中提及,或者使用上面代码中显示的过滤器。 please list your problem in more detail format. 请以更详细的格式列出您的问题。 including data , app.js code and some html. 包括data,app.js代码和一些html。 good luck. 祝好运。

This is what i came up with. 这就是我想出的。 You should really provide a plunker with some data yourself next time. 下次您应该为自己提供一些数据。

The interesting part for ng options is the list comprehension like style in ng选项的有趣部分是列表理解,例如样式

ng-options="sD.shipModeId as sD.description for sD in shipModeArr"

Edit: 编辑:

ng-options="sD.shipModeId as format(sD) for sD in shipModeArr"

In Controller: 在控制器中:

$scope.format = function(sd){
    return sd.description
    + ' Arrives '
    + sd.estArrivalDate + ' '
    + $filter('currency')(sd.shipCharge);
};

You can define yourself a function to get your objects formatted nicely. 您可以为自己定义一个函数,以很好地格式化对象。 I edited the plunker to show you where to put the pieces. 我编辑了插棒,向您展示了放置碎片的位置。 If you expand the $scope.format function to return your formatting this should work. 如果展开$ scope.format函数以返回格式,则此方法应该有效。

If you don't want an empty selection you will have to select an initial value. 如果您不希望有一个空选择,则必须选择一个初始值。

Edit2: EDIT2:

I updated your fiddle . 我更新了你的小提琴

plnkr plnkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM