Avatar of justaphase
justaphase
Flag 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?
JavaScriptWeb FrameworksScripting Languages

Avatar of undefined
Last Comment
justaphase

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gelonida

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ambience

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

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..
ambience

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
justaphase

ASKER
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
justaphase

ASKER
Thx! worked!