Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm trying to create a simple application using AngularJS and TimelineJS3 but I'm having a problem with it.

I have a state (timeline) which contains a partial view (timeline.html) associated with a controller. This state contains a promise to fetch data from the server, which is going to be stored in the $scope variable inside the controller. The problem is that I need to access this variable inside a <script> tag in the partial view file.

Here's the code:

app.js

    app.config(['$stateProvider', '$urlRouterProvider', 
      function($stateProvider, $urlRouterProvider){
      .state('timeline', {
        url: '/timelines/:id',
        views: {
          'partial-timeline': {
            templateUrl: 'partial/timeline.html',
            controller: 'TimelineController'
          }
        },
        resolve: {
          getOneTimeline: ['$stateParams','timelineServ', function($stateParams, timelineServ) {
            return timelineServ.getTimelineById($stateParams.id);
          }]
        }
      });
    }]);

    app.controller('TimelineController', ['$scope', 'timelineServ', 
      function($scope, timelineServ) {
      $scope.timelineData = timelineServ.indivTimeline;
    }]);

timeline.html

    {{timelineData}}
    <div id="timeline-embed" style="width: 100%; height: 600px"></div>

    <script type="text/javascript">
      window.timeline = new TL.Timeline('timeline-embed', {{timelineData}});
    </script>

From the {{timelineData}} expression outside I can see that the variable has the correct data however, as I said, I'm not able to use it inside the <script> tags.

What is the best approach to solve this problem? I'm quite new to AngularJS.

Thank you in advance. Best Regards!

share|improve this question
    
something like this needs to be intialized in a directive so that element exists when code is run – charlietfl 18 hours ago
    
You can get a reference to the scope via var scope = angular.element(elementInsideTheControllerScope).scope() - then access your variable: scope.timelineData – tymeJV 18 hours ago
    
using it like this var scope = angular.element('div[ng-controller="TimelineController"]').scope() and then accessing scope.timelineData I get that the data is undefined. Although, when logging the scope to the console, it contains a timelineData field with the correct data! – Delais 18 hours ago
    
How can I use it in a directive? @charlietfl – Delais 18 hours ago

1 Answer 1

You can do this in the controller OR the directive: after the promise is resolved, save the result on a global window variable and use that in your tag.

app.controller('TimelineController', ['$scope', 'timelineServ', 
  function($scope, timelineServ) {
  window.timelineData = timelineServ.indivTimeline;
}]);

<script type="text/javascript">
  window.timeline = new TL.Timeline('timeline-embed', window.timelineData);
</script>

Remember that you need to initialze the TL.Timeline after the promise has been resolved in some way.

share|improve this answer
    
Thank you for your answer but, as I apply it, I'm getting that the data is undefined! – Delais 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.