两个下拉过滤器。第一个确定第二个信息

时间:2015-01-16 02:09:13

标签: javascript angularjs drop-down-menu underscore.js

我不知道如何构建这个,虽然我花了几个小时尝试。我的代码似乎有点接近,但我无法将我的选择从HTML传递回控制器。

My Angular code:

 var Categories = Parse.Object.extend("bizCategories");

        $scope.categories = [
            {category: "Food and Drink"},
            {category: "Healthcare"},
            {category: "Recreation"},
            {category: "Service"},
            {category: "Shops"},
            {category: "Travel"}
        ]

        $scope.setPrimary = function (category) {
            $scope.offer.primary = category;
            $scope.secondaryCategories(category);
        };

        $scope.secondaryCategories = function(category){
            var Secondary = new Parse.Query(Categories);
            Secondary.limit(1000);
            Secondary.find({
                success:function(list){
                    $scope.SecondaryCategories = _.uniq(_.filter(_.map(list, function(item){
                        return{primary: item.get('secondary')
                        }}
                    )),'primary',true)
                    console.log($scope.SecondaryCategories);
                },
                error: function(error){alert("A mistake was made.");
                }
            })
        }

我的HTML:

  <select id= 'primary' ng-model="offer.primary" ng-options="XXX.category for XXX in categories"
                ng-click= setPrimary(category)>
            <option value="">Primary Business Category</option>
        </select>

        <select id= 'secondary' ng-model="offer.secondary" ng-disabled="!offer.primary"
                ng-options="XXX.category for XXX in SecondaryCategories" ng-click="setSecondary('XXX.category')">
            <option value="">Secondary Business Category</option>
        </select>

你会看到我在我的html中为ng-options选择了XXX。我选择了这个,因为从我从教程中可以看出,特别是把它称为任何东西的目的是任意的,我把这个XXX作为一个不是那个问题。此外,我需要从第一个下拉列表中的信息转移到第二个下拉列表(类别)的函数,但这显示为未定义。

数据来源:

Primary is the first selection criteria and exists as array in the first controller. Secondary is filtered using primary; it's a many to many relationship.

1 个答案:

答案 0 :(得分:2)

您似乎尝试在选择1时设置其他下拉列表。您应该使用ng-change 代替ng-click

尝试: -

    <select id= 'primary' ng-model="offer.primary" ng-options="XXX.category for XXX in categories"
            ng-change= setPrimary(category)>
        <option value="">Primary Business Category</option>
    </select>

    <select id= 'secondary' ng-model="offer.secondary" ng-disabled="!offer.primary"
            ng-options="XXX.category for XXX in SecondaryCategories" ng-change="setSecondary('XXX.category')">
        <option value="">Secondary Business Category</option>
    </select>

由于您的ng-model已绑定到offer.primary,因此您可以避免执行$scope.offer.primary = category;,因为已经设置了ng-model。