在下拉列表中选择默认选项,将值保存在下拉列表的value属性中。 Angular.js。 Angular.js [英] Select default option in a dropdown, save the value in the value attribute of the dropdown. Angular.js. Angular.js

查看:88
本文介绍了在下拉列表中选择默认选项,将值保存在下拉列表的value属性中。 Angular.js。 Angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到无限的帖子试图在下拉菜单中默认选择一个选项。但我无法实现它。我有一些国家。

I have seen infinity of post to try to choose by default an option in a dropdown. But I have not been able to achieve it. I have an array of some countries.

$scope.regions = 
 [
      {
        name: "COLOMBIA",
        code: 5
      },
      {
        name: "ARGENTINA",
        code: 6
      },
      {
        name: "BRAZIL",
        code: 7
      }
  ];

我有这个变量:

$scope.selectThisId={
    "id":6,
    "animal":'dog',
    "variable":'xxx32'
 };

我需要下拉值等于

$scope.region=$scope.selectThisId.id;

变量。

http://plnkr.co / edit / nmc8iLz6BIFac0Swfth8?p =预览

推荐答案

工作演示: http://plnkr.co/edit/zhye91pDUEMgaZnXZGWE?p=preview

你基本上通过这样做来创建一个默认区域

You basically create a default region by doing

$scope.select = $scope.selectThisId.id;
$scope.regionSelected = {};

然后用<$创建一个 select 标签c $ c> ng-options ,将其绑定到模型,并在模型上调用 ng-init

then create a select tag with ng-options, binding it to the model, and calling ng-init on the model.

<select ng-options="item as item.name for item in regions track by item.code" 
        ng-model="regionSelected" ng-init="regionSelected.code = select">
</select>

这篇关于在下拉列表中选择默认选项,将值保存在下拉列表的value属性中。 Angular.js。 Angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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