Convert server-side repeated content into
ngRepeat compatible with some restrictions.
ServerRepeat module as a dependency of your app.
serverRepeat directive in your server-side content using
ngRepeat's short syntax.
This will generate a
posts array in
PostsController scope, and every post will have a child scope with a reference to the current post in the
post property, as if rendered client side using
Having a child scope for each member of the collection allows you to "angularize" each item independently. As an example let's hide each post summary and add a link in each post to show it.
Without a child scope, the snippet above would hide all post summaries as expected, but clicking on any link would display all the summaries instead of the selected one.
In the previous example, each child scope has a
post empty object. If you need to use a post's data you can use the
Now we have the html rendered content parsed and populated into the
post object in each child scope.
server-bind works as
ng-bind, so changing the value of a binded property like the example below will reflect the property update in the DOM.
Adding aditional data that is not rendered in the DOM
server-bind directive can be used in the same element that uses the
server-repeat directive. In this case, it will expect a JSON representation with properties that will be extended to the
Each child scope have
$odd variables as in
We found in the way that we wanted to add functionality to our "repeated" html snippets, but we didn't want to immerse ourselves in a big refactor to render this data client side using
ngRepeat. Doing this would also prevent the search engines to index our content.
Caveats and restrictions
- Changes that modify the collection's length (adding or removing items) won't be reflected in the DOM.
- For the moment you can only bind "flat level" properties using
server-bind. Deep level properties will be considered for a future version.
Copyright (c) 2015 Restorando
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.