一尘不染

在AngularJS中切换数据模型以获取动态选择菜单

angularjs

我正在尝试做的是拥有三个不同的 _< 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

当我从第 一个 选择菜单中选择“品牌舱口盖”或“银石”时,其他两个菜单的数据将更改/更新以与正确的数据相对应。我正在使用 $ 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/


阅读 273

收藏
2020-07-04

共1个答案

一尘不染

看看我在这里做了什么: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" }
            ]
        }
    ];
}]);
2020-07-04