I am experienced as a developer but a beginner in angularjs and I have not used javascript for a while.
My site is separated into several angularjs apps. But two apps could have to display the same "entity" types.
For example my first app is dedicated to books and would receive from an api (for example:http://example.com/Books) the following json data:
[
{ "id":"1", "title":"...", "publisher":"..." },
{ "id":"2", "title":"...", "publisher":"..." },
]
The second app is more general and would receive from an api (for example:http://example.com/Things) the following json data:
{
"count":"3",
"items":
// my two books
{ "type":"book", "data":{ "id":"1", "title":"...", "publisher":"..." } },
{ "type":"book", "data":{ "id":"2", "title":"...", "publisher":"..." } },
// and other things
{ "type":"movie", "data":{ "id":"45", "title":"...", "producer":"..." } },
]
}
I created a template "book.html" :
<div>{{book.title}} ({{book.publisher}})</div>
I use it in my view with :
<ul><li ng-repeat="book in books" ng-include="'book.html'"/></ul>
So far so good...
I now would like to use the same template - without duplicating it - to display the books in the first case and in the second with something like :
<ul><li ng-repeat="item in items" ng-include="item.type+'.html'"/></ul>
My issue is not dynamic templates. My issue is: in my book.html template, I use "book.title", but for my second app, I would need to use "item.data.title".
The ideal, in my mind, would have been something like :
<ul><li ng-repeat="item in items" ng-include="item.type+'.html'" ng-something="book=item.data"/></ul>
I could resolve it by "converting" my books array into the second format. But I think I misunderstood something and perhaps I am using angularjs in a wrong way.
Could you please give me some clues ?
Thanks
ng-initwork? docs.angularjs.org/api/ng/directive/ngInit