AngularJS ng-repeat 设置默认选择值 [英] AngularJS ng-repeat setting default select value

查看:29
本文介绍了AngularJS ng-repeat 设置默认选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$scope.activities =
    [
        { id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
        { id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
    ];

<select data-ng-model="engineer.currentActivity" data-ng-options="a.name for a in activities">                
</select>

使用上述内容,我可以创建一个包含 3 个值的选择框,一个空白的,然后是下拉菜单和水平栏.

Using the above I am able to create a select box with 3 values, a blank one and then dropdown menu and horizontal bar.

我想将水平栏设置为我的默认值,但不知道如何执行此操作.

I would like to set Horizontal Bar as my default and cannot see how to do this.

帮助会很棒

推荐答案

在控制器中,只需添加一行设置初始选择:

In the controller, just add a line to setup the initial selection:

$scope.activities =
    [
        { id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
        { id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
    ];

$scope.engineer = {}; // if needed
$scope.engineer.currentActivity = $scope.activities[1];

这篇关于AngularJS ng-repeat 设置默认选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆