简体   繁体   English

选择/选项ng-model未与ng-selected绑定

[英]select/options ng-model is not bound with ng-selected

JSFiddle 的jsfiddle

I have this AngularJS html for a select/options with a selected item by default according to an external value (selectedGroup). 我有这个AngularJS html用于选择/选项,默认情况下是根据外部值(selectedGroup)选择的。 There should be an empty option for this list ('-ROOT-' option): 此列表应该有一个空选项(“ -ROOT-”选项):

<select class="form-control" ng-model="newDoor.groupId">
    <option value="">-ROOT-</option>
    <option ng-repeat="group in groups" ng-value="group.id" ng-selected="group==selectedGroup">
        {{group.name}}
    </option>
</select>

The problem is: when ng-selected condition causes an option to be selected, the ng-model is not affected by this select, but when u change the option, ng-model get the value. 问题是:当ng-selected条件导致一个选项被选中时,ng-model不受此选择的影响,但是当您更改选项时,ng-model将获得该值。 How to let the value of ng-model take the auto selected option directly? 如何让ng-model的值直接采用自动选择的选项?

You should use ngOptions . 您应该使用ngOptions ngOptions is designed to populate select inputs, using a microsyntax passed as an attribute. ngOptions旨在使用作为属性传递的微语法来填充选择输入。

For example, in your case, you could use the following: 例如,您可以使用以下方法:

<select ng-model="newDoor.groupId" 
        ng-options="group.id as group.name for group in groups">

This would list every group in groups using group.id as the value and displaying group.name as the name. 这会列出每个groupgroups使用group.id作为值并显示group.name作为名称。 It handles the change detection for you. 它为您处理更改检测。

The only extra thing you would need to do, is account for this requirement: 您唯一需要做的就是考虑这一要求:

There should be an empty option for this list ('-ROOT-' option): 此列表应该有一个空选项(“ -ROOT-”选项):

I have done this by adding another option to the front of your groups array: 我通过在groups数组的前面添加另一个选项来完成此操作:

$scope.groups.unshift({ name: '-ROOT-', id: -1 });

Finally, to make sure something real is selected when the scope initialises, this line sets newDoor.groupId : 最后,为了确保在初始化范围时选择了真实的东西,此行设置了newDoor.groupId

$scope.newDoor = { groupId: $scope.groups[0].id }

See an updated fiddle here 在这里查看更新的小提琴

You can achieve this in the controller by predefining it first as a new object and then putting the value when you putting the value for selectedGroup. 您可以在控制器中实现此目的,方法是先将其预定义为新对象,然后在为selectedGroup放置值时放置值。

Code: 码:

function doorsTreeController($scope) {
    $scope.newDoor = {};
    $scope.groups = [{
        name: 'G1',
        id: 1
    }, {
        name: 'G2',
        id: 2
    }, {
        name: 'G3',
        id: 3
    }, {
        name: 'G4',
        id: 4
    }];
    $scope.selectedGroup = $scope.groups[1];
    $scope.newDoor.groupId = $scope.selectedGroup.id;
}

Working Fiddle 工作小提琴

OR 要么

have a ng-init in html and rest of your code as is. 在html中有一个ng-init,其余的代码保持不变。

<div ng-app>
    <div ng-controller='doorsTreeController'>
        <select class="form-control" ng-init="newDoor.groupId = selectedGroup.id" ng-model="newDoor.groupId">
            <option value="">-ROOT-</option>
            <option ng-repeat="group in groups" ng-value="group.id" ng-selected="group==selectedGroup">{{group.name}}</option>
    </select>Selected groupId is: {{newDoor.groupId}}</div>

Updated Fiddle 更新小提琴

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

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