We use ng-init to do something, that with the updated docs on it sounds
like being a hack, but in our scenario of migrating from an old app to an
SPA it has been really useful, and I can imagine that it could stay useful
even after the full migration.
What we essentially do is:
<div ng-controller="appController as app" ng-init="app.initialize(@razorVar1,
'@razorVar2')"></div>
@razorVar1 and @razorVar2 would be rendered at the server, so the actual
outcome when the client gets it could be:
<div ng-controller="appController as app" ng-init="app.initialize(42,
'spock')"></div>
Alternatives I can think of is:
------------------------------
a) Render a script block that initializes the to values as angular
constants, values or in a config or run block.
<script>angular.module('myApp').value('razorVar1',
@razorVar1);angular.module('myApp').constant('razorVar2',
@razorVar2);angular.module('myApp').config(['razorVarsProvider', function(rvp) {
rvp.set('razorVar1', @razorVar1);
rvp.set('razorVar2', @razorVar2);}]);</script>
But I find that to be far less clean than the ng-init approach. (very
verbose in comparison) without bringing any added value.
------------------------------
b) Request them from a REST API, which I am also avoiding as it would add
another http request that I find unnecessary when we have the values right
there when rendering the view on the server.
angular.module('myApp').controller('appController ', ['$http', function(http) {
var self = this;
http.get('/razorValues').success(function(val) {
self.razorVar1 = val.razorVar1;
self.razorVar2= val.razorVar2;
});}]);
------------------------------
Question is, what is the appropriate approach, from the Core teams point of
view?... And if it's none of the above, what other alternatives is there
that was intended for it?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.