1

Sorry to ask this question again but no answer or workaround has been identified.

This is a follow-up of this question : Stackoverflow

I have a plunker which describes the issue (basically putting a tree structure in a dropdown list) :

Plunker example

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.

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;

};})

Your help would be very much appreciated! Thanks

1
  • could you describe more about what's not working? Commented Jul 1, 2014 at 14:18

1 Answer 1

3

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).

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>  

demo: http://plnkr.co/edit/J8PyypC61uzx8PPNV6Zu?p=preview

Sign up to request clarification or add additional context in comments.

3 Comments

So, I did all this hard work for nothing ? Grrr...
@ExpertSystem Sorry! ;)
Thanks a lot to you two! I understand the issue now. Indeed I could have just replaced the flattenTree part with flatTree.push(tree[i]) and that would have kept the binding. I understand Dayan Moreno Leon's answer much better now. Guys you have ended my one-week battle with this. I wish you an awesome day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.