10

我想做的是拥有三个不同的<select>菜单,它们都将绑定到相同的数据中。更改第一个选择菜单,将更改菜单 2 和 3 的数据。

这是我的控制器的内部:

$scope.data = [
        {
            "id" : "0",
            "site" : "Brands Hatch",
            "buildings" : [
                { "building" : "Building #1" },
                { "building" : "Building #2" },
                { "building" : "Building #3" }
            ],
            "floors" : [
                { "floor" : "Floor #1" },
                { "floor" : "Floor #2" },
                { "floor" : "Floor #3" }
            ]
        },{
            "id" : "1",
            "site" : "Silverstone",
            "buildings" : [
                { "building" : "Building #4" },
                { "building" : "Building #5" },
                { "building" : "Building #6" }
            ],
            "floors" : [
                { "floor" : "Floor #4" },
                { "floor" : "Floor #5" },
                { "floor" : "Floor #6" }
            ]
        }
    ];

到目前为止,这是我从参考中尝试过的,它使用了我需要的相同想法:http: //codepen.io/adnan-i/pen/gLtap

当我从第一个选择菜单中选择“Brands Hatch”或“Silverstone”时,其他两个菜单的数据将更改/更新以对应正确的数据。我正在使用$watch来监听我从上面的 CodePen 链接中获取的更改。

这是观看脚本(未修改且显然不起作用):

$scope.$watch('selected.id', function(id){
        delete $scope.selected.value;
        angular.forEach($scope.data, function(attr){
            if(attr.id === id){
                $scope.selectedAttr = attr;
            }
        });
    });

据我所知,这会删除更改时的当前数据,然后循环通过$scope.data,如果 attr.id 与传递给函数的 id 匹配,它将数据推回更新视图的范围。我只是真的坚持构建这个并且希望得到一些指导和帮助,因为我对 AngularJS 真的很陌生。谢谢!:)

如果有人可以提供帮助,请使用 jsFiddle 进行完整的工作:http: //jsfiddle.net/sgdea/

4

1 回答 1

17

看看我在这里做了什么:http: //jsfiddle.net/sgdea/2/

您根本不需要使用$watch- 您只需让每个依赖选择的输入引用父项中的选择。

请注意ng-options第二个和第三个选择参考如何selected.site由第一个选择设置:

<div ng-app="myApp" ng-controller="BookingCtrl">
    <select ng-model="selected.site"
            ng-options="s.site for s in data">
        <option value="">-- Site --</option>
    </select>
    <select ng-model="selected.building"
            ng-options="b.building for b in selected.site.buildings">
        <option value="">-- Building --</option>
    </select>
    <select ng-model="selected.floor"
            ng-options="f.floor for f in selected.site.floors">
        <option value="">-- Floor --</option>
    </select>
</div>

我在 javascript 中所做的只是删除你的$watch

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

myApp.controller( 'BookingCtrl', ['$scope', '$location', function ( $scope, $location ) {

    $scope.selected = {};

    $scope.data = [
        {
            "id" : "0",
            "site" : "Brands Hatch",
            "buildings" : [
                { "building" : "Building #1" },
                { "building" : "Building #2" },
                { "building" : "Building #3" }
            ],
            "floors" : [
                { "floor" : "Floor #1" },
                { "floor" : "Floor #2" },
                { "floor" : "Floor #3" }
            ]
        },{
            "id" : "1",
            "site" : "Silverstone",
            "buildings" : [
                { "building" : "Building #4" },
                { "building" : "Building #5" },
                { "building" : "Building #6" }
            ],
            "floors" : [
                { "floor" : "Floor #4" },
                { "floor" : "Floor #5" },
                { "floor" : "Floor #6" }
            ]
        }
    ];
}]);
于 2013-07-17T13:38:45.360 回答