You should use any dependency/module loader like requirejs to make your dependency loading more organized in application.
To load script dynamically in DOM, you can use a function like this -
var loadScript = function (url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
console.log(url + ' loaded successfully.');
if ($.isFunction(callback)) {
callback();
}
}
};
} else { //Others
script.onload = function () {
console.log(url + ' loaded successfully.');
if ($.isFunction(callback)) {
callback();
}
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
This function should reside at any script that is loaded upfront (i.e. config.js). You can avoid the second param if you don't need. It just gives you a facility to do something if you need once after the script is loaded.
Here's a demo call to script loading function using your myApp -
var myApp = angular.module('myApp', ['ionic','ionic-datepicker','ui.router','toaster','ngAnimate']);
myApp.run(function(){
config.loadScript('yourScriptPath', null);
})
.config(function($stateProvider, $urlRouterProvider) {
//==========================================================================//
// Build the pages up for the application with their respective controllers //
//==========================================================================//
$stateProvider
.state('login', {
cache : false,
url : '/',
controller : "LoginController",
templateUrl : 'login.html'
})
.state('page1', {
cache : false,
url : '/page1',
controller : "page1controller",
templateUrl : 'page1.html'
})
.state('page2', {
cache : false,
url : '/page2',
controller : "page2controller",
templateUrl : 'page2.html'
})
.state('page3', {
cache : false,
url : '/page3',
controller : "page3controller",
templateUrl : 'page3.html'
});
$urlRouterProvider.otherwise('/');
});
Thanks