md-select检查md-options中重复选项的标准是什么

时间:2015-12-09 05:57:12

标签: angularjs angular-material

我在项目中使用Angular Material一段时间了。在使用md-select时,我遇到了一个问题,即我得到重复的md-option值错误。

我知道md-options采用唯一值,我正在为md-options分配一个数组。但是,这是一个对象数组。所以我想知道用于区分对象的标准是什么。 API没有多说。

我的用例要求根据另一个md-select的选择更改md-select的md-options。因此,我正在观看第一个md-select的选择,并在其更改时触发监视并更新第二个md-select的md-options。

以下是我用于将数组分配给md-options的方法:

$scope.$watch('search.selectedTrades', function(newTrades, oldTrades) {
    if ((newTrades.length === 0)) {
        $rootScope.search.selectedTrades = oldTrades;
        return;
    }
    if ($rootScope.search.selectedTrades && $rootScope.search.selectedTrades.length > 0) {
        if (!$rootScope.identity.isClusterManager) {
            $rootScope.search.selectedTrades = newTrades;
            SearchFilterData.setSelectedTrades(newTrades);
            $rootScope.search.selectedClusters = [];
            $scope.clusters = [];
            $scope.subareas = [];
            var clusterKeys = [];
            $rootScope.search.selectedTrades.forEach(function(t) {
                t.lstClusters.forEach(function(c) {
                    if (clusterKeys.indexOf(c.ClusterKey) == -1) {
                        clusterKeys.push(c.ClusterKey);
                        $scope.clusters.push(c);
                    }
                })
            })
        }
    } else {
        $scope.clusters = [];
        $scope.subareas = [];
        $rootScope.search.selectedClusters = [];
        $rootScope.search.selectedSubAreas = [];
        SearchFilterData.setSelectedTrades($rootScope.search.selectedTrades);
    }
});

在上面的代码中,clusterKey是每个对象的唯一实体。所以我用它来将唯一值推送到数组中。 然而,在我选择和取消选择各种选项之后,这在几个随机场景中发生。请告知我做错了什么以及标记两个对象重复的标准是什么

2 个答案:

答案 0 :(得分:5)

你没有提供你的标记,所以我不能确定,但​​在我的情况下,这个问题是由于省略了'值上的双重曲线造成的。 md-option标记中的属性。

这很糟糕:注意缺少花括号

<md-option ng-repeat="item in vm.list" value="item.id">{{item.text}}</md-option>

这不是:

<md-option ng-repeat="item in vm.itemlist" value="{{item.id}}">{{item.text}}</md-option>

我认为这个失败的原因是每个项目都会被放入选项列表中,并且会给出一个值为&item 39的项目。 (直译)。它将在重复的第二次迭代中失败。 使用花括号会导致&#39; item.id&#39;中的值。使用。

希望这有帮助。

答案 1 :(得分:2)

尝试使用ng-value而不仅仅是value属性。

<md-option ng-repeat="item in vm.list" ng-value="{{item.id}}">{{item.text}}</md-option>