0

How do i get data in $scope.moduleSelected into label in the treedata_avm array instead of the hardcoded values present in the array?

app.controller('treeController',['$http','$scope','dataService',function( $http,$scope,dataService){

    $http.get('WS/tree').success(function(data,status){
        $scope.modules=data;
        $scope.moduleSelected = $scope.modules[0].MODULE_NAME;
        $scope.moduleSelectedId = $scope.modules[0].ID;
        $scope.moduleSelectedParentId = $scope.modules[0].PARENT_ID;

     console.log($scope.modules);
     console.log($scope.moduleSelected);
     console.log($scope.moduleSelectedId);
     console.log($scope.moduleSelectedParentId);


    }).error(function(data){
        $scope.modules=data || "Request failed";
        console.log("Request failed "+ $scope.modules);
    });

    }]);

treedata_avm:

treedata_avm = [
  {
    label: 'Animal',
    children: [
      {
        label: 'Dog',

      }, {
        label: 'Cat',

      }, {
        label: 'Hippopotamus',

      }, {
        label: 'Chicken',
        children: ['White Leghorn', 'Rhode Island Red', 'Jersey Giant']
      }
    ]
  }]
1
  • How do i get data in $scope.moduleSelected into label in the treedata_avm array instead of the hardcoded values present in the array? Commented Feb 12, 2016 at 12:42

2 Answers 2

0

You can listen to event 'onchange' on the field, that's bound to $scope.moduleSelected and change your json when input field is changed. But I can't see, why would you need something like this?

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

Comments

0

$http with its success promise resolve function returns data in the current format: response.data

Meaning in your case you should first set

$http.get('WS/tree').success(function(response){
    $scope.modules = response.data;
    $scope.treedata_avm = response.data
}).error(function (){...})

If you are going to use your treedata_avm on a view, then it should be on the $scope as in $scope.treedata_avm if not then just use it as a variable inside the controller

Also my advice would be to put all of this $http logic into a provider (factory, service, provider) and then invoke it from there. The advice is to make controllers as thin as possible and put the logic inside the providers (factories, services, providers), thus making it reausable

Hope this helps.

Comments

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.