简体   繁体   English

angularjs-级联选择从第一次选择中使用模型数据

[英]angularjs - cascading selects use model data from first select

I'm attempting to get at the version data stored inside the server model, however, it's not playing ball. 我试图获取存储在server模型中的version数据,但是,它没有发挥作用。

My assumption is that on load the initial data from the first select is not yet available because it hasn't really been selected (technically speaking). 我的假设是,从加载时开始,首次选择的初始数据尚不可用,因为尚未真正选择(从技术上来说)。 I tried to use trigger('click') whether this would help at all, but it did absolutely nothing. 我尝试使用trigger('click')是否完全有帮助,但是它什么也没做。 I can't seem to find any answers out there and I have a feeling I maybe tackling it from the wrong end. 我似乎找不到任何答案,而且我觉得我可能会从错误的角度解决它。

edit: Forgot to mention, if I have to restructure my data to allow for this to happen, so be it. 编辑:忘记提及了,如果我必须重组我的数据以允许发生这种情况,那就这样吧。

Here's all my code + JSFiddle: 这是我所有的代码+ JSFiddle:

http://jsfiddle.net/h8uoy9xr/3/ http://jsfiddle.net/h8uoy9xr/3/

HTML: HTML:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app>
    <div ng-controller="MyCntrl">
        <div class="selection" ng-repeat="selection in selections">
            <select ng-model="server" ng-options="server as server.name for server in servers track by server.id" ng-init="server.id=selection.server"></select>
            {{ server | json }}
            <select ng-model="version" ng-options="version as version.name for version in server.version track by version.id" ng-init="version.id=selection.version"></select>
        </div>
    </div>
</div>

JS: JS:

function MyCntrl($scope) {

    $scope.servers = [
        {
           "id": 1,
           "name": "server1",
           "version":
           [
               {
                   id:1,
                   name: "10.x"
               },
               {
                   id:3,name: "12.x"
               }
           ]
        },
        {
           "id": 2,
           "name": "server2",
           "version":
           [
               {
                   id: 2,
                   name: "1.0"
               },
               {
                   id: 3,
                   name: "2.0"
               }
           ]
        }
    ];

    $scope.selections = [
        {
            server: 2,
            version: 3
        },
        {
            server: 1,
            version: 3
        }
    ];

}

cascading dropdownlist inside ng-repeat ng-repeat中的级联dropdownlist

Here's a working solution of the above problem. 这是上述问题的可行解决方案。 I hadn't realized angularjs did things a wee bit differently with the models, but finally it clicked. 我没有意识到angularjs在模型上所做的事情有些不同,但是最终它被点击了。 See below code + fiddle for anyone needing something similar in the future: 请参阅下面的代码+摆弄将来需要类似功能的人:

Fiddle: http://jsfiddle.net/mmy6z57g/2/ 小提琴: http : //jsfiddle.net/mmy6z57g/2/

Fiddle: http://jsfiddle.net/mmy6z57g/3/ (added a button to demonstrate how additional servers can be added) 小提琴: http : //jsfiddle.net/mmy6z57g/3/ (添加了一个按钮来演示如何添加其他服务器)

HTML: HTML:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<div ng-app="test">
    <div ng-controller="MyCntrl">
        <div class="selection" ng-repeat="xserver in xservers">
            <select ng-model="xservers[$index]" ng-options="server as server.name for server in servers track by server.id"></select>
            <select ng-model="xversions[$index]" ng-options="version as version.name for version in xservers[$index].version track by version.id"></select>
        </div>
    </div>
</div>

JS: JS:

var app = angular.module('test', []);

app.controller('MyCntrl', function($scope) {
    $scope.servers = [
        {
           id: 1,
           name: "server1",
           version:
           [
               {
                   id:1,
                   name: "10.x"
               },
               {
                   id:3,
                   name: "12.x"
               }
           ]
        },
        {
           id: 2,
           name: "server2",
           version:
           [
               {
                   id: 2,
                   name: "1.0"
               },
               {
                   id: 3,
                   name: "2.0"
               }
           ]
        }
    ];

    $scope.xservers = [
        {
            id: 2,
            name: "server2",
            version:
           [
               {
                   id: 2,
                   name: "1.0"
               },
               {
                   id: 3,
                   name: "2.0"
               }
           ]
        },
        {
            id: 1,
            name: "server1",
            version:
           [
               {
                   id:1,
                   name: "10.x"
               },
               {
                   id:3,
                   name: "12.x"
               }
           ]
        }
    ];

    $scope.xversions = [
        {
           id: 3,
           name: "2.0"
        },
        {
            id: 3,
            name: "12.x"
        }
    ];
});

Could you use your controller and use a .then(); 您可以使用控制器并使用.then();吗? so that it selects the first one and when that is done then it selects the second? 这样它就选择第一个,然后选择第二个? User wouldn't see it and it would solve the issue of second one not getting selected. 用户不会看到它,它将解决第二个未被选中的问题。

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

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