1
在我的后端,我有一个嵌套的类别结构如下所示:如何填充AngularJS嵌套类别下拉列表?
Category
--------
id
parent_id
name
即我完全加载到$scope.categories
为一维数组。
在屏幕上,我想显示每个级别的下拉菜单,显示相应的路径。例如,如果路径车/轿车/福特/金牛座,我想显示四个下拉菜单:
汽车v轿车v福特v金牛座v
每当更改下拉列表时,应该删除右侧的那个,并且立即填充所有子类别,其父类型是我刚选择的那个类别。定期的东西。
我还创建了一个category_path
数组,其中包含产品类别路径中的每个category_id:[null, category_id, category_id, category_id]
。
然后,我这样做:
<select
ng-repeat="item in category_path"
ng-model="selected_category[$index]"
ng-options="category.name for category in (categories | filter : { parent_id: item } : true) track by category.id "
ng-change="select_category(selected_category[$index], $index)"
></select>
在我的控制器:
$scope.select_category = function (selected_category, index) {
if ($filter('filter') ($scope.categories, { parent_id: selected_category.id }, true).length) {
if ($scope.category_path[index+1] != undefined) {
$scope.category_path = $scope.category_path.slice(0, index+1);
$scope.category_path[index+1] = selected_category.id;
} else {
$scope.category_path.push(selected_category.id);
}
} else {
$scope.product.category_id = selected_category.id;
}
}
这就像魅力,除了事实,我无法填充的默认值每个下拉列表中,基于当前产品category_path
阵列。
我想:
ng-init="select_category[$index] = category_path[$index]"
没有成功。想法?
你可以在控制器中将'category_path'数组复制到'select_category'吗?您提供的功能之前还是之后?这应该在包含控制器时执行,以便列表应该正确初始化。 – ShellFish
@ShellFish你的意思是'$ scope.selected_category = angular.copy($ scope.category_path);'?这样做,它没有奏效。 – Mauro
不,我的意思是'angular.copy($ scope.category_path,$ scope.selected_category);'。请确保**不**包含此*内*功能。 – ShellFish