Angualr js directive to preload data from the server

I needed to preload a twig template with data server side but required the data to be available from within the angualr js app.

This directive looks for a pre-load attribute then simply loads the ng-model found on the element with the preload data. The directive also checks that the ng-model path follows std javascript naming conventions… in case some clever dude figures out a way to hack the directive with their own dodgy code.

With this directive in place the following sets the ng-model with the data in the preload attr:

As the directive builds a path to the scope based on the ng-model, you can go as deep into the expected object as needed, eg:

Leave a Reply

Your email address will not be published. Required fields are marked *