Link to home
Start Free TrialLog in
Avatar of justaphase
justaphaseFlag for Portugal

asked on

Angularjs - How to use expression inside script tag

Hi everyone,

How can i do this?:
    <div class="row" ng-controller="singleEventCtrl">
    
        <div class="col-md-9"> ...... more html code .....

                <script type="application/ld+json">
                {
                  "@context": "http://schema.org",
                  "@type": "Event",
                  "name": '{{event[0].titulo}}',
                  "startDate" : "{{event[0].event_dataini_iso8601 | date:'yyyy-MM-dd'}}",
                  "endDate" : "{{event[0].event_dataini_iso8601 | date:'yyyy-MM-dd'}}",
                  "url" : "http://example.com/tourdates.html",
                  "location" :
                  {
                    "@type" : "Place",
                    "name" : "{{event[0].tb_town_townname}}"
                  }
                }
                </script>
        </div>
 </div>

Open in new window


Angular doesn't render nothing inside the script tag, and i need that for google.
Is it possible?
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can try doing something like, though I do think it may require further improvements since it has to execute after the scope has been created.

<script type="javascript">
                // Get to Angular scope
                var div = document.querySelector('[ng-controller=singleEventCtrl]');
                var theScope= angular.element(div ).scope();
                var event = theScope.<Whatever>;
</script>

<script type="application/ld+json">
                {
                  "@context": "http://schema.org",
                  "@type": "Event",
                  "name": '{{event[0].titulo}}',
                  "startDate" : "{{event[0].event_dataini_iso8601 | date:'yyyy-MM-dd'}}",
                  "endDate" : "{{event[0].event_dataini_iso8601 | date:'yyyy-MM-dd'}}",
                  "url" : "http://example.com/tourdates.html",
                  "location" :
                  {
                    "@type" : "Place",
                    "name" : "{{event[0].tb_town_townname}}"
                  }
                }
 </script>

Open in new window

Avatar of justaphase

ASKER

ambience,

in the DOM, the {{event[0].something}} is note replaced with the value in the DOM.. doesn't work :\
Not outside the controller or inside..

As to gelonida answer i think it may work, but gonna need some time to implement and test..
Oh OK, sorry I forgot to update that part. I was thinking of changing it to

<script type="application/ld+json">
                {
                  "@context": "http://schema.org",
                  "@type": "Event",
                  "name": event[0].titulo,

This snippet now uses the object var event, which is fetched from angular scope in the other script block.
ambience, it may work if you get the values with javascript, but not for google.
google is not going to execute javascript to get the values, he is going to get the values inside the brackets 'some value' and not event[0].titulo.

I know google is able to get the page rendered after Angularjs finishes it, i tested it.
But i don't think is going to get this json with the values like that... because if you make inspect element in google chrome you can see the {{event.something}} transformed in real values. But not the: "name": event[0].titulo. inside the script tag.
It stays event[0].titulo
Thx! worked!