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?
LVL 1
justaphaseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gelonidaCommented:
Up to my knowledge AngularJS does no substitution within (java)script tags.

You might look at following article:
It creates an Angular directive, that dynamically injects script tags into the  DOM.

http://odetocode.com/blogs/scott/archive/2014/04/08/dynamically-injecting-script-tags-with-angularjs.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambienceCommented:
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

0
justaphaseAuthor Commented:
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..
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ambienceCommented:
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.
0
justaphaseAuthor Commented:
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
0
justaphaseAuthor Commented:
Thx! worked!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.