如何设置默认值以在我使用NG模型和NG变化时选择



我问这个问题,因为ng模型覆盖select tag中的默认值。因此,我需要将<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>
        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

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

或更改事件后的控制器侧

$scope.sortedBy= yourDefaultValue;

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

$scope.sortedBy = "your default value";

最新更新