Omkar Mone
Mega Sage

All,

Recently I came across as post on community where, the requirement was to access the server script variable in the <script> section in the HTML code.

 As data.var_name or c.data.var_name are not accessible in the <script>, they throw data and c not defined error.

It was obvious that these we not actually known to the scope of the HTML code.

Now reading the AngularJS docs somewhere I came across a variable called 'top' that is accessible in the script section in the HTML as it the $window kind of object.

I made a following tweak to make this work.

Hope this helps everyone, also some easy way to do this is much appreciated.

 

HTML - 

<div>

  <script>

    console.log(top.Val);

  </script>

</div>

 

Client Controller - 

function($scope) {

  /* widget controller */

  var c = this; 

top.Val = c.data.valuefromserver;

 

}

 

Server Script - 

(function() {

    data.valuefromserver = "test";

})();

 

 

Here, as at the Server Side, we won’t be able to access the top variable as it is a window object, I had to do manipulations at the client side and push the data needed in the top object in order to access it in the <script> section of HTML.

 

Suggestions are always appreciated😊

 

If this article helped you in any way, please then bookmark it or mark it as helpful.

 

Regards

Omkar Mone

2019 Community Leader.

2020 Community MVP.