I am in the process of changing an application I wrote in Play Framework 2.5 to a single page application using AngularJs.
Here's an overview of what I was doing:
- display a list of posts, using Scala Template's @for
- each post was rendered using an included template
snippet:
@for(post <- posts) {
@fragments.post(post)
}
And so far, here is what I did with Angular:
- use $http to retrieve data from DB using Scala's javascript routing
- use ng-repeat instead of the @for, to iterate over the list
- each post is currently rendered as plain json, using {{post}}
snippet:
<ul>
<li ng-repeat="post in posts">
<div> {{post}} </div>
</li>
</ul>
and now having trouble displaying each post with some template, here are the attempts:
- somehow pass post as JSON to the Scala template I was using initially. I tried changing the template's parameter type from Post (scala object) to some Json. Naturally, the code below caused a "not found: value post"
e.g.:
<li ng-repeat="post in posts">
@fragments.post( {{post}} )
</li>
- use Angular directive template instead of Scala template: In the angularjs code below, using the inline template worked fine. I am also able to externalize the template to some other file, using the templateUrl option.
e.g.:
<ul>
<li ng-repeat="post in posts">
<div mydirective></div>
</li>
</ul>
and the js:
mainApp.directive("mydirective", function() {
return {
//template: "<h1>Made by a directive: {{post.title}} !</h1>"
templateUrl: "/assets/path_to_template"
};
});
Even though the latter is now working, I am not even sure which approach would be better. I do really like being able to pass entire forms to the Scala template and let it bind the UI form to the controller form. This integration of AngularJs into Play Framework has been really confusing, and the Play-angular-seed did not make it any clearer to me.
What would be the best way of achieving the above?
Thanks