I have been trying to teach myself both JavaScript and AngularJS, so please forgive any foolish mistakes, wrong terminology or the fact that I may be approaching this totally the wrong way. I also apologise if a solution has been posted before, but after searching relentlessly I cannot find anything.
I am putting together a storybook as a single page app using AngularJS. The first few divs, which animate in and out, via an index calculation contain a few lines of static html but then the reader can choose one of four paths that the story will take by clicking one of four buttons.
To accomplish this I have set-up four JSON files, that will dynamically load depending on a button click. For example, if the reader clicks the chocolate ice cream button, the chocolate.json file loads and rest of the story will be about chocolate ice cream. If the reader clicks the strawberry ice cream button the strawberry.json file loads and the rest of the story is about strawberry ice cream etc. (I am simplifying the content concept here!)
This works really well. To me, at least, it seems sensible to store the content in JSON format.
However, there are a couple of variables that I would like to include throughout the story to make it more personal to the reader. For example, perhaps their first name that can be recalled throughout the narrative.
This is where I am hitting a problem. All of my AngularJS expressions are binding perfectly in the html file where the content is non-JSON. But of course, since most of my content is from external JSON files, can I place expressions within these?
For example,
In my html AngularJS text binding expressions such as (simplified):
<p>{{readerName}}</p>
<p>{{myContent.intro}}</p>
Work perfectly. Pulling in the expected values, or data from one of the JSON files.
But what if I want to include values in the external JSON file itself, and then pull that in? The example below, understandably, just results in a text string, the expressions do not evaluate. So, literally {{readerName}}, rather than the John Smith value I was hoping for. How do I get the expressions to show values?
[
{
"textline" : "Hey {{readerName}}, what's your favourite ice cream flavour",
"answers" : [
{ "id" : 0, "text" : "Chocolate" },
{ "id" : 1, "text" : "Chocolate chip" },
{ "id" : 2, "text" : "Double Chocolate" },
{ "id" : 3, "text" : "Dark Chocolate" }
],
"feedback" : "That's a lot of chocolate {{readerName}}..."
},
{
"textline" : "Hey {{readerName}}, what's your second favourite ice cream flavour",
"answers" : [
{"id" : 0, "text" : "Milk Chocolate" },
{"id" : 1, "text" : "Belgian Chocolate" },
{"id" : 2, "text" : "Death by Chocolate" },
{"id" : 3, "text" : "Rich Chocolate" }
],
"feedback" : "That's even more chocolate {{readerName}}!"
}
]
Thanks in advance for any answers. I have a suspicion I am approaching this the wrong way...