簡體   English   中英

當我使用ng-model和ng-change時如何設置默認值以進行選擇?

[英]How to set default value to select when I am using ng-model and and ng-change?

我問這個問題是因為ng-model覆蓋了select標簽中的默認值。 因此,我需要設置<option value="-translation.published_at" selected>{{ 'Newest first' | translate }}</option> <option value="-translation.published_at" selected>{{ 'Newest first' | translate }}</option>轉換為默認值。 這是我的代碼:

    <div class="filter__order filter--select p-rel">
        <select id="order" class="text-dark-gray" ng-model="sortedBy" ng-change="filterByDate(sortedBy)">
            <option value="-translation.published_at" selected>{{ 'Newest first' | translate }}</option>
            <option value="translation.published_at">{{ 'Oldest first' | translate }}</option>
        </select>
    </div>

應用ng-init

<select id="order" class="text-dark-gray" ng-model="sortedBy" ng-init="sortedBy= yourDefaultValue" ng-change="filterByDate(sortedBy)">

更改事件后在控制器端進行或操作

$scope.sortedBy= yourDefaultValue;
        Assign value to the ng-model of select

    <select required="required" class="form-control control-sm"  ng-change="settingtestValue(ItemValue)" ng-model="ItemValue" name="info_bcsWorkItem">
<option value="">--Select--</option>
<option ng-repeat="bcsItem in bcsWorkItemList" value="{{ bcsItem }}" >{{ bcsItem }}</option>
</select>

$scope.ItemValue= "your value you want to assign";

您可以使用ng-init或內部角度控制器:

$scope.sortedBy = "your default value";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM