Is it possible to have scope variable access inside <head> tag?

219 views
Skip to first unread message

lbecke...@gmail.com

unread,
Jan 5, 2016, 3:06:22 PM1/5/16
to AngularJS
I have pretty much followed the code linked here, except my service and controller are in their own .js files. I have config.properties file with JSON (2 key/value pairs...nothing too crazy) and I need them inside my html file like so:

<html ng-app="myApp" ng-controller="ConfigCtrl">
   
<head>
       
<!-- Piwik -->
        <script type="text/javascript">
       
var _paq = _paq || [];
       _paq
.push(['trackPageView']);
       _paq
.push(['enableLinkTracking']);
       
(function() {
       
var u={{piwikUrl}};
       _paq
.push(['setTrackerUrl', u+'piwik.php']);
      _paq
.push(['setSiteId', {{piwikSiteId}}]);
       
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
       g
.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
       
})();
       </script>
       <!-- End Piwik Code -->
</head>

 However, it seems that you can't use a scope variable in the head. I get the error "Uncaught SyntaxError: Unexpected token {" for the line where {{piwikUrl}} is located in the code above. If it is in quotations, then my the URL being provided to the new <script> element being created becomes "{{piwikUrl}}piwik.js". Is it possible to use scope variables inside the head tag/script tag? If so, how do I do it?

Sander Elias

unread,
Jan 5, 2016, 11:19:54 PM1/5/16
to AngularJS
Hi,

There are a number of issues with your approach. First one, your inline script will run first. Angular hasn't even booted by that time, so it's impossible to expect angular to parse any expression. Also, only expressions inside HTML will be parsed, not expressions inside scripts.

Regards
Sander

Ankush Joshi

unread,
Jan 7, 2016, 1:01:21 AM1/7/16
to AngularJS
Hi...

As per my knowledge and opinion, I think that scope variable will not be accessible inside head tag, As while in head code browser doesnt knows that its angular js / or whatever scripting language is !!

So, Scope will be only accessible where controllers are reachable 
Reply all
Reply to author
Forward
0 new messages