我在我的项目中使用Angular材质已经有一段时间了。在使用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是每个对象的唯一实体。因此,我使用它将唯一值推入数组。然而,在我选择和取消选择各种选项之后,这种情况发生在一些随机的场景中。请告诉我我做错了什么,以及标记两个对象重复的标准是什么
发布于 2016-01-26 18:57:54
您没有提供您的标记,所以我不能确定,但在我的例子中,问题是由于在md-option标记中省略了'value‘属性上的双curley。
这很糟糕:请注意缺少的大括号
<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.id‘(字面意思)的值。它将在重复的第二次迭代中失败。使用大括号会导致使用“item.id”中的值。
希望这能有所帮助。
发布于 2016-07-28 19:28:13
尝试使用ng-value而不仅仅是value属性。
<md-option ng-repeat="item in vm.list" ng-value="item.id">{{item.text}}</md-option>
https://stackoverflow.com/questions/34171709
复制相似问题