{"id":22379,"date":"2015-07-02T23:21:14","date_gmt":"2015-07-02T17:51:14","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=22379"},"modified":"2017-08-03T13:19:23","modified_gmt":"2017-08-03T07:49:23","slug":"manually-bootstrapping-an-angularjs-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/manually-bootstrapping-an-angularjs-application\/","title":{"rendered":"Manually Bootstrapping an AngularJS Application"},"content":{"rendered":"<p>We always need some mechanism to control our front-end application at the time when we bootstrap it. Getting some customization done before kicking in the <a title=\"Building Intuitive Frontend Interfaces with AngularJS\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">angular on the front end<\/a>, can be a desired use case for some of the Angular Applications. It is mainly needed when we have to redirect to an Angular application from some other social login sites(Facebook, twitter, etc).<\/p>\n<p>The reason that we need to make changes before bootstrapping the Angular on the front-end is that, these social login sites append few characters to the redirecting urls such as the one below :<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\n&lt;www.domain.com&gt;\/app#<br \/>\n&lt;www.domain.com&gt;\/app\/#_=_<br \/>\n&lt;www.domain.com&gt;\/app#\/<br \/>\n[\/code]<\/p>\n<p>So, we need to remove these extra appended characters from the url, before starting up the application on the font-end. As it may cause ambiguity for the angular route provider to serve the correct route.<\/p>\n<p>Now we have to remove these appended characters before bootstrapping up the angular application. You can achieve it by following these 2 simple steps:<\/p>\n<p><strong>Step 1:<\/strong> Initialize the Angular on the front-end.<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nvar app = angular.module(&#8216;myApp&#8217;, []);<br \/>\n[\/code]<\/p>\n<p><strong>Step 2:<\/strong> Once the document gets ready, perform your required customization task. And when you are done with all the changes, then you can bootstrap the angular.<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nangular.bootstrap(document, [&quot;myApp&quot;]);<br \/>\n[\/code]<\/p>\n<p>This will <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">setup the angular application<\/a> onto the front-end.<\/p>\n<p>So, the sample code for the above mentioned use case will some what look like this:<\/p>\n<p>[code language=&#8221;javascript&#8221;]<br \/>\nangular.element(document).ready(function() {<br \/>\n\tif (window.location.hash === &#8216;#\/&#8217; || window.location.hash === &#8216;#_=_&#8217;) {<br \/>\n\t\twindow.location.href = window.location.origin + &#8216;\/&#8217;;<br \/>\n\t} else if (window.location.href[window.location.href.length &#8211; 1] === &#8216;#&#8217;) {<br \/>\n\t\twindow.location.href = window.location.origin + &#8216;\/&#8217;;<br \/>\n\t} else {<br \/>\n\t\t\/\/Then init the app<br \/>\n\t\tangular.bootstrap(document, [&quot;myApp&quot;]);<br \/>\n\t}<br \/>\n});<br \/>\n[\/code]<\/p>\n<p>It gives you the power, to setup the ground before you kick start your Angular on front-end. There are many more things that you can perform by this method of bootstrapping the Angular application.<\/p>\n<p>Hope this helps!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We always need some mechanism to control our front-end application at the time when we bootstrap it. Getting some customization done before kicking in the angular on the front end, can be a desired use case for some of the Angular Applications. It is mainly needed when we have to redirect to an Angular application [&hellip;]<\/p>\n","protected":false},"author":99,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1439,3429,1],"tags":[3955,955,4697,55],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22379"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/99"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=22379"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22379\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=22379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=22379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=22379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}