angularjs NG选项自定义过滤器不工作 [英] angularjs ng-options with custom filter not working
问题描述
不好意思再问这个问题,但没有答案或解决方法已被确定。
Sorry to ask this question again but no answer or workaround has been identified.
这是这个问题的后续:<一href=\"http://stackoverflow.com/questions/24414417/angularjs-select-with-filter-leaves-first-line-empty-but-not-when-filter-is-appl\">Stackoverflow
我描述这一问题的plunker(基本上把一个树形结构中的一个下拉列表):
I have a plunker which describes the issue (basically putting a tree structure in a dropdown list) :
如图所示,第一选择不选择的价值,并得到回一个空行 - 这也是一个谜
As illustrated the first select does not select the value and gets back to an empty line - which is also a mystery.
我一直没能找到任何类似的情况,我很接近放弃,所以这是最后的机会了。
I have not been able to find any similar cases and I am close to giving up so this is the last chance.
重点code区是过滤器和initTreeSelect功能:
Key code areas are the filter and the initTreeSelect function :
$scope.initTreeSelect = function(tree){
$scope.filteredFields = $filter('flattenTree')(tree);
return $scope.filteredFields[0];
};
angular.module('myApp.filters', []).filter('flattenTree', function (OrgSvc) {
return function (array) {
var arrayToReturn = [];
arrayToReturn = OrgSvc.flattenTree(array);
return arrayToReturn;
};})
您的帮助将非常AP preciated!
谢谢
Your help would be very much appreciated! Thanks
推荐答案
什么大雁莫雷诺Leon在他的回答中写道实际上是正确的。而真正的问题是,角度保持丢失模型数据的轨迹(因为/阵列由引用标识默认的对象)。
What Dayan Moreno Leon wrote in his answer is actually correct. And the problem really is that angular keeps losing track of the model data (because by default objects/arrays are identified by reference).
这是挫败,因为你的 flattenTree
保持返回新创建的对象:
This is thwarted, because your flattenTree
keeps returning newly created objects:
flatTree.push({id:tree[i].id, display:tree[i].display, parentId:tree[i].parentId});
有关虽然你的具体问题,我认为使用轨道由
将有所帮助,例如:
For your concrete problem though, I think using track by
will help, e.g.:
<select
class='form-control'
ng-model="treeValue1"
ng-init="treeValue1 = initTreeSelect(fields)"
ng-options="element as element.display for element in fields | flattenTree track by element.id"
></select>
演示: http://plnkr.co/edit/J8PyypC61uzx8PPNV6Zu?p=preVIEW
这篇关于angularjs NG选项自定义过滤器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!