无论选择了哪个选项,如何使用 ng-options 设置相同的文本 - Angular.js

How to use ng-options to set the same text no matter the option selected - Angular.js

我了解如何设置选项框的默认值。

我的问题是不同的,因为有一个默认值 "Actions" - 还有 3 个其他过滤选项可以选择...

    $scope.actions = [
  {display_name:"Actions", type:"unknown", box_display: "Actions"},
  {display_name:"Mark all incoming voicemails as read", type:"read", box_display: "Actions"},
  {display_name:"Mark all incoming voicemails as unread", type:"unread", box_display: "Actions"},
  {display_name:"Delete all read incoming voicemails", type:"delete", box_display: "Actions"},
    ];
$scope.selectedAction = $scope.actions[0];
<select class="XButton XButtonNormal XButtonDropDown disabled-false" ng-model="selectedAction"
            ng-options = "action_option.display_name for action_option in actions"
            ng-change="handleVoiceMailAction(selectedAction.type)"
        >
            <option>Actions</option>

无论选择数组中的 4 个对象中的哪一个,如何让选择框显示 "Actions"。选择选项时,不同的选项仍然需要可见(并且功能需要与适当的选择相对应),但我希望该框在进行选择后继续显示 "Actions"。

这就是您要找的东西:

模板:

<select ng-model="selectedAction" ng-options="action.display_name for action in actions" 
                                  ng-change="setCurrentAction(selectedAction)"></select>
<p>Current action : {{currentAction}}</p>

控制器:

$scope.actions = [{
    display_name: "Actions",
    type: "unknown"
}, {
    display_name: "Mark all incoming voicemails as read",
    type: "read"
}, {
    display_name: "Mark all incoming voicemails as unread",
    type: "unread"
}, {
    display_name: "Delete all read incoming voicemails",
    type: "delete"
}];

$scope.selectedAction = $scope.currentAction = $scope.actions[0];

$scope.setCurrentAction = function (action) {
    $scope.currentAction = action;
    $scope.selectedAction = $scope.actions[0];
};

fiddle

解释:

因为要显示'Action'绑定到下拉列表ng-model属性的动作,我们必须区分selectedAction下拉视图 ,以及为您的应用程序逻辑选择的操作。我将后一个动作称为 currentAction。在选择事件中,我们必须注册所选动作到当前动作。