简体   繁体   中英

AngularJS: Pass data from from ng-init to <script>

This is how code looks:

<div ng-init="companyData">
  <script type="text/javascript">
    window.timeline = new TL.Timeline('timeline-embed', sampleJson);
  </script>
</div>

Is there a way to pass companyData in ng-init to the timeline ? I want something like this:

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

Try to init new TL.Timeline() inside your controller logic. I try to avoid using ng-init because it always ends in to much logic on the "view"-side of your application. Handle that kind of stuff in your controller would be much more tidy.

view

<div ng-controller="MyCtrl"></div>

AngularJS App

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.companyData = 'someData';
    window.timeline = new TL.Timeline('timeline-embed', $scope.companyData);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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