如何设置默认值以在我使用ng-model和ng-change时选择?

时间:2018-02-16 10:15:39

标签: javascript angularjs html5

我问的是这个问题,因为ng-model会覆盖select标签中的默认值。所以我需要将<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>

3 个答案:

答案 0 :(得分:1)

申请ng-init

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

更改事件后控制器端的OR

$scope.sortedBy= yourDefaultValue;

答案 1 :(得分:1)

        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";

答案 2 :(得分:0)

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

$scope.sortedBy = "your default value";