0

in my application controller I make a request to my api. It looks like this :

.state('state1', {
      url: '/datas/:id',
      templateUrl: 'myurl.com',
      title: 'title',
            controller: function($http, $rootScope, $location,  $stateParams){
              var id = $stateParams.id;               
        $http.post('http://' + $location.host() + id, {id: id } , {})
        .then(function(d){

          $rootScope.data = d.data;
        },
        function(err){
          console.log(err);
        });
      },
    })

my d3 script is something like this :

<script>
  ...
   var force = d3.layout.force().size([width, height]).on("tick", tick);

   var svg = d3.select("#d3g").append("svg").attr("width", width).attr("height", height);
   var link = svg.selectAll(".link"),
        node = svg.selectAll(".node");
  ...
  ...
  d3.json("data.json", function(error, json) {
              if (error){
                console.log(error);
              } 
    ...
    ...
</script>

How can I pass data I receive from api (in the controller) to my d3 display (in and html file).

2
  • Well you don't need to do d3.json since you have already done the Ajax in the controller , so you will need to move the code inside d3.json success block into the then function of the promise Commented Nov 27, 2016 at 10:00
  • @Cyril thank, now how to i have access to data ? Commented Nov 27, 2016 at 10:24

1 Answer 1

1

More "angular way" is loadind data in a service and creating a directive to render your d3 component like:

.service('dataLoader', function($http, $location,  $stateParams){
    return {
        get: function() {
            var id = $stateParams.id;               
            return $http
                .post('http://' + $location.host() + id, {id: id } , {})
                .then(function(d){
                    return d.data;
                })
                .catch(function(err){
                  return err;
                });
            }
    }
})

.directive('mapElement', function(){
  return {
    restrict: 'E'
    scope: {},
    controller: function(dataLoader) {
       ...
       var force = d3.layout.force().size([width, height]).on("tick", tick);

       var svg = d3.select("#d3g").append("svg").attr("width", width).attr("height", height);
       var link = svg.selectAll(".link"),
            node = svg.selectAll(".node");
      ...
      ...
      dataLoader
        .get()
        .then(function(data){
            d3.json(data, function(error, json) {
                  if (error){
                    console.log(error);
                  } 
                ...
                ...
            }
        })
  } 
});

To usage this directive, just put in your html:

<map-element></map-element>
Sign up to request clarification or add additional context in comments.

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.