简体   繁体   English

在AngularJS中的脚本内访问范围变量

[英]Access scope variable inside script in AngularJS

I'm trying to create a simple application using AngularJS and TimelineJS3 but I'm having a problem with it. 我正在尝试使用AngularJS和TimelineJS3创建一个简单的应用程序,但是我遇到了问题。

I have a state ( timeline ) which contains a partial view ( timeline.html ) associated with a controller. 我有一个状态( timeline ),其中包含与控制器相关联的局部视图( timeline.html )。 This state contains a promise to fetch data from the server, which is going to be stored in the $scope variable inside the controller. 此状态包含从服务器获取数据的承诺,该承诺将存储在控制器内的$scope变量中。 The problem is that I need to access this variable inside a <script> tag in the partial view file. 问题是我需要在部分视图文件的<script>标记内访问此变量。

Here's the code: 这是代码:

app.js 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 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. 从外面的{{timelineData}}表达式中,我可以看到该变量具有正确的数据,但是正如我所说,我无法在<script>标记内使用它。

What is the best approach to solve this problem? 解决此问题的最佳方法是什么? I'm quite new to AngularJS. 我对AngularJS很陌生。

Thank you in advance. 先感谢您。 Best Regards! 最好的祝福!

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. 请记住,在以某种方式解决了承诺之后 ,您需要初始化TL.Timeline。

I managed to make it work :) 我设法使其工作:)

On app.js , the code related to the definition of the state timeline remained the same. app.js上 ,与状态时间轴的定义相关的代码保持不变。 Additionally, I created a new directive: 另外,我创建了一个新指令:

app.directive('runTimelineScript', function() {
  return {
    restrict: 'E',
    link: function (scope, element, attr) {
      window.timeline = new TL.Timeline('timeline-embed', timelineData);
    }
  };
});

And modified the controller to look like this: 并将控制器修改为如下所示:

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

Then, in the html file for the partial view ( timeline.html ), I just inserted the new directive: 然后,在局部视图的HTML文件( timeline.html )中,我刚刚插入了新指令:

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

<run-timeline-script></run-timeline-script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM