{"id":1698,"date":"2022-08-30T15:18:42","date_gmt":"2022-08-30T15:18:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/29\/run-angular-js-as-a-callback-ajax-for-example-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:42","modified_gmt":"2022-08-30T15:18:42","slug":"run-angular-js-as-a-callback-ajax-for-example-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/run-angular-js-as-a-callback-ajax-for-example-collection-of-common-programming-errors\/","title":{"rendered":"Run angular.js as a callback ($.ajax, for example)-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m writing an app with mostly two javascripts, resources_loader.js and app.js, where the first loads some json files used by app.js.<\/p>\n<p>The problem is: app.js (which has angular.js stuff) should run only after resources_loader.js. I tried to run angular code inside the success callback (resources_loader contains a deferred), but this not seems to work well. Here&#8217;s my index.html:<\/p>\n<pre><code>\n\n\n    \n    \n\n\n<\/code><\/pre>\n<p>Consider that I moved app.js to resources_loader, inside the success callback. Everytime I try to run it, angular raises the following exception: <code>Uncaught Error: No module: myapp<\/code>.<\/p>\n<p>My guess is that <code>angular.module('myapp', [])<\/code> should run before onload&#8217;s event, which is not the case here.<\/p>\n<p>Another thing is that I want to use yepnope.js in my project like this:<\/p>\n<pre><code>\n\n\n    var loader = load_stuff();\n    yepnope({\n        test: loader.resolved(),\n        yep: ['angular.js', 'app.js', 'foo.js', 'bar.js'],\n        nope: ['fail.js']\n    });\n\n<\/code><\/pre>\n<p><code>app.js<\/code> contains angular code. I think it&#8217;s more performant because it only loads angular if the resources are loaded. But how can I do it?<\/p>\n<ol>\n<li>\n<p>Remove <code>ng-app<\/code> from tag and use <code>angular.bootstrap(document, ['myapp']);<\/code> to fire it up once your resources are loaded, like this:<\/p>\n<pre><code>var app = angular.module('myapp', []);\n\napp.config(['$routeProvider', '$locationProvider', function($router, $location) {\n    $location.html5Mode(true);\n\n    $router\n    .when('\/', {\n        templateUrl: 'some_template.html',\n        controller: 'myController'\n    });\n}]);\n\n\/\/ and then, after everything, run\nangular.bootstrap(document, ['myapp']);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-29 06:06:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing an app with mostly two javascripts, resources_loader.js and app.js, where the first loads some json files used by app.js. The problem is: app.js (which has angular.js stuff) should run only after resources_loader.js. I tried to run angular code inside the success callback (resources_loader contains a deferred), but this not seems to work well. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1698","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1698"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1698\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}