GVKun编程网logo

如何在ng-options中设置默认值(ng-options怎么用)

12

如果您对如何在ng-options中设置默认值感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何在ng-options中设置默认值的详细内容,我们还将为您解答ng-opti

如果您对如何在ng-options中设置默认值感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何在ng-options中设置默认值的详细内容,我们还将为您解答ng-options怎么用的相关问题,并且为您提供关于angular 的 select 中 ng-options 设置默认值、angularjs – 在ngSelect中设置默认值、angularjs – 如何在md-select中设置默认值、angularjs – 如何在角度js中设置默认时区的有价值信息。

本文目录一览:

如何在ng-options中设置默认值(ng-options怎么用)

如何在ng-options中设置默认值(ng-options怎么用)

我可以在angularjs中使用默认值设置一个下拉列表,

 <select name="repeatSelect" id="repeatSelect" ng-model="repeatSelect" ng-init=" repeatSelect = data[0].id">    <option ng-repeat="option in data" value="{{option.id}}">{{option.name}}</option> </select>

如何使用来达到相同的效果ng-options?我胆战心惊,

 <select name="repeatSelect"    id="repeatSelect"    ng-model="repeatSelect"   ng-init=" repeatSelect = option.id"     ng-options="option.name for option in data track by option.id"></select>

但是没用。样品小提琴在这里

答案1

小编典典

使用ng-init 为设定的默认值ng-options

这是: 演示

<select name="repeatSelect"    id="repeatSelect"    ng-model="repeatSelect"   ng-init=" repeatSelect = data[0].id"     ng-options="option.id as option.name for option in data">          </select>

angular 的 select 中 ng-options 设置默认值

angular 的 select 中 ng-options 设置默认值

angular 的 select 中 ng-options 设置默认值

AngularJS 开发的后台web系统中,经常需要对后台数据进行修改,而在修改数据的过程中,若页面中有select下拉框,需要让其选中原来的数据,即select中需要设置默认值。

默认值设置方法:

  1. select中使用ng-model绑定数据myColor

  2. 在对应的controller中定义ng-model全局数据$scope.myColor,并为其赋值,该值即为select的默认值。

代码如下:

angular.module(''myApp'', [])
  .controller(''MyController'', [''$scope''], function($scope) {
    $scope.colors = [''black'', ''white'', ''red'', ''blue'', ''yellow''];
    
    $scope.myColor = $scope.colors[2];
  })
<div ng-controller="MyController">
  <select ng-model="myColor" ng-options="color for color in colors"></select>
</div>

显示效果:
图片描述

说明: 该文档参考angular官方api

angularjs – 在ngSelect中设置默认值

angularjs – 在ngSelect中设置默认值

我有一个ngSelect,里面有一些选项.
<select data-ng-model="type" data-ng-change="option(type)">
   <option data-ng-repeat="type in languages" value="{{type.i18n}}">
        {{type.language}}
    </option>
</select>

还有一个控制器

angular.module('navigation',[])
.controller('NavCtrl',['$scope','$translate',function($scope,$translate){

    $scope.option = function(type){
        console.log(type) //this display the i18n value of languages
        $translate.use(type);
    }

    $scope.languages = [
        { language: "English",i18n: "en_EN"},{ language: "Swedish",i18n : "se_SE" }
    ];
}])

我希望ngSelect有一个默认选项,在我的情况下:“英语”.我试着把它设置为:

$scope.type = $scope.languages[0].language; // English
$scope.type = $scope.languages[0]; //The whole darn json object.

请帮忙?

试试这种方式
<select  ng-model="selectedlanguage" ng-change="option(this.selectedlanguage)"  ng-options="i.language for i in languages">
            </select>

// Js代码

angular.module('navigation',i18n : "se_SE" }
    ];

    **$scope.selectedlanguage = $scope.languages[0];**
}])

angularjs – 如何在md-select中设置默认值

angularjs – 如何在md-select中设置默认值

这是我的角度代码,我想将默认值设置为300mb
<label >RAM</label>
  <md-select ng-model="RAMVal">
    <md-option value="300mb" ng-selected="index == 1">300mb</md-option>
    <md-option value="400mb">400mb</md-option>
  </md-select>
只需添加这个
ng-selected="true"

你可以登记入住
https://jsfiddle.net/rschmukler/0ju750xo/

angularjs – 如何在角度js中设置默认时区

angularjs – 如何在角度js中设置默认时区

我想在日期字段中设置默认时区偏移量(美国/芝加哥).新的Date函数添加到代码中并将其正确调用到 HTML文件中.它正确显示日期和时间,但我想将时区默认设置为America / chicago.
控制器中的代码:

<script>
    mainApp.controller('depositController',['$scope','$http','$filter',function($scope,$http,$filter) {	
          $scope.myDate = new Date();	
    }]); 	
</script>

HTML代码:

<div>
  <input id="deposit_date" name="deposit_date"type="text" 
        ng-init="deposit_time=(myDate | date:'HH:mm:ss a' : '-0500')" 
        ng-model="deposit_time" 
        readonly="readonly"/>
</div>

我想显示America / Chicago(Central Standard Time)当前时间.

解决方法

你不能.这取决于浏览器设置

  • The AngularJS datetime filter uses the time zone settings of the browser.
  • The same application will show different time information depending on the time zone settings of the computer that the application is running on.
  • Neither JavaScript nor AngularJS currently supports displaying the date with a timezone specified by the developer

今天的关于如何在ng-options中设置默认值ng-options怎么用的分享已经结束,谢谢您的关注,如果想了解更多关于angular 的 select 中 ng-options 设置默认值、angularjs – 在ngSelect中设置默认值、angularjs – 如何在md-select中设置默认值、angularjs – 如何在角度js中设置默认时区的相关知识,请在本站进行查询。

本文标签: