简体   繁体   English

AngularJs - SELECT中的ng-model

[英]AngularJs - ng-model in a SELECT

JSfiddle 的jsfiddle

Problem: 问题:

I have a SELECT-element in my page, which is filled in with an ng-repeat . 我的页面中有一个SELECT元素,其中填充了ng-repeat It also has a ng-model which has a default value. 它还有一个具有默认值的ng-model

When I change the value, the ng-model adapts, that's ok. 当我更改值时, ng-model适应,这没关系。 But the dropdown-list shows an empty slot at launch, where it should have the item with the default value selected instead. 但是下拉列表在启动时显示一个空槽,它应该具有选择默认值的项目。

Code

<div ng-app ng-controller="myCtrl">
    <select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
         <option ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
    </select>
</div>

With JS: 用JS:

function myCtrl ($scope) {
    $scope.units = [
        {'id': 10, 'label': 'test1'},
        {'id': 27, 'label': 'test2'},
        {'id': 39, 'label': 'test3'},
    ]

        $scope.data = {
        'id': 1,
        'unit': 27
        }

};

You can use the ng-selected directive on the option elements. 您可以在选项元素上使用ng-selected指令。 It takes expression that if truthy will set the selected property. 需要表达的是,如果truthy将设置所选属性。

In this case: 在这种情况下:

<option ng-selected="data.unit == item.id" 
        ng-repeat="item in units" 
        ng-value="item.id">{{item.label}}</option>

Demo 演示

 angular.module("app",[]).controller("myCtrl",function($scope) { $scope.units = [ {'id': 10, 'label': 'test1'}, {'id': 27, 'label': 'test2'}, {'id': 39, 'label': 'test3'}, ] $scope.data = { 'id': 1, 'unit': 27 } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="myCtrl"> <select class="form-control" ng-change="unitChanged()" ng-model="data.unit"> <option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option> </select> </div> 

try the following code : 尝试以下代码:

In your controller : 在你的控制器中:

 function myCtrl ($scope) {
      $scope.units = [
         {'id': 10, 'label': 'test1'},
         {'id': 27, 'label': 'test2'},
         {'id': 39, 'label': 'test3'},
      ];

   $scope.data= $scope.units[0]; // Set by default the value "test1"
 };

In your page : 在您的页面中:

 <select ng-model="data" ng-options="opt as opt.label for opt in units ">
                </select>

Working Fiddle 工作小提琴

您不需要定义选项标记,您可以使用ngOptions指令执行此操作: https ://docs.angularjs.org/api/ng/directive/ngOptions

<select class="form-control" ng-change="unitChanged()" ng-model="data.unit" ng-options="unit.id as unit.label for unit in units"></select>

However, ngOptions provides some benefits such as reducing memory and increasing speed by not creating a new scope for each repeated instance. 但是,ngOptions提供了一些好处,例如通过不为每个重复实例创建新范围来减少内存和提高速度。 angular docs 角度文档

Alternative solution is use ng-init directive. 替代解决方案是使用ng-init指令。 You can specify function that will be initialize your default data. 您可以指定将初始化默认数据的函数。

$scope.init = function(){
            angular.forEach($scope.units, function(item){
                if(item.id === $scope.data.unit){
                    $scope.data.unit = item;
                }
            });
        } 

See jsfiddle jsfiddle

You can also put the item with the default value selected out of the ng-repeat like follow : 您还可以将具有从ng-repeat中选择的默认值的项目放入,如下所示:

<div ng-app="app" ng-controller="myCtrl">
    <select class="form-control" ng-change="unitChanged()" ng-model="data.unit">
        <option value="yourDefaultValue">Default one</option>
        <option ng-selected="data.unit == item.id" ng-repeat="item in units" ng-value="item.id">{{item.label}}</option>
    </select>
</div>

and don't forget the value atribute if you leave it blank you will have the same issue. 如果你把它留空,不要忘记价值属性,你会遇到同样的问题。

Select's default value should be one of its value in the list. Select的默认值应该是列表中的值之一。 In order to load the select with default value you can use ng-options . 要使用默认值加载选择,您可以使用ng-options A scope variable need to be set in the controller and that variable is assigned as ng-model in HTML's select tag. 需要在控制器中设置范围变量,并将该变量指定为HTML的选择标记中的ng-model

View this plunker for any references: 查看此plunker以获取任何参考:

http://embed.plnkr.co/hQiYqaQXrtpxQ96gcZhq/preview http://embed.plnkr.co/hQiYqaQXrtpxQ96gcZhq/preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM