{"id":73,"date":"2015-06-02T12:44:45","date_gmt":"2015-06-02T16:44:45","guid":{"rendered":"http:\/\/george.hotelling.net\/91percent\/?p=73"},"modified":"2015-06-02T12:44:45","modified_gmt":"2015-06-02T16:44:45","slug":"passing-configuration-to-angular","status":"publish","type":"post","link":"https:\/\/george.hotelling.net\/91percent\/2015\/06\/02\/passing-configuration-to-angular\/","title":{"rendered":"Passing configuration to Angular"},"content":{"rendered":"<p>This is something that we got wrong on our project initially, then had a guy named Mark come on board and nail a very clean solution.<\/p>\n<p>What we were trying to accomplish: we wanted to give our Angular Singe Page App some configuration data for initialization. Things like a CSRF token and API URL, so not necessarily things we could load from a web service.<\/p>\n<p><strong>The wrong way to do it:<\/strong><\/p>\n<p>We started off using an <code>ng-init<\/code> on our element. If you <a href=\"https:\/\/docs.angularjs.org\/api\/ng\/directive\/ngInit\">RTFM on <code>ng-init<\/code><\/a> they make it very clear that you should not be using it for that purpose. In our defense, the name &#8220;init&#8221; is right there in the name and the warning wasn&#8217;t as bright red in earlier versions of the documentation.<\/p>\n<p><strong>A better way to do it:<\/strong><\/p>\n<p>What we are doing now is putting this in our server-side template:<\/p>\n<pre><code>&lt;script&gt;\nangular.module('ns-config', [])\n    .constant('config', {{config|js}});\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>and then inject the <code>ns-config<\/code> module into our project. By using Angular&#8217;s <code>constant()<\/code> instead of <code>value()<\/code> the <code>config<\/code> object is available in our application&#8217;s <code>.config()<\/code> block.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is something that we got wrong on our project initially, then had a guy named Mark come on board and nail a very clean solution. What we were trying to accomplish: we wanted to give our Angular Singe Page App some configuration data for initialization. Things like a CSRF token and API URL, so &hellip; <a href=\"https:\/\/george.hotelling.net\/91percent\/2015\/06\/02\/passing-configuration-to-angular\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Passing configuration to Angular<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[6],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-programming","tag-angular"],"_links":{"self":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":3,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts\/73\/revisions\/95"}],"wp:attachment":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}