I'm using angular ui routing to load templates inside a view. Each template has it's own javascript code specific for that page. Is there a way to load the template specific javascript code/file the moment that the template is opened? At the moment I have to add all the javascript code/files for every template in the main page.
Another solution is to use an iframe. This allows the page to load it's own javascript files. But this way the navigation doesn't work as good (page is reset when refreshed).
Example:
index.html
<!DOCTYPE html>
<html ng-app="testApp">
<head>
<title></title>
<meta charset="utf-8" />
<script src="angular.min.js"></script>
<script src="angular-ui-router.min.js"></script>
<script src="app.js"></script>
<script src="template1Functions.js"></script>
<script src="template2Functions.js"></script>
</head>
<body>
<a ui-sref="template1">Template 1</a>
<a ui-sref="template2">Template 2</a>
<div ui-view></div>
</body>
</html>
template1.html
<button onclick='template1TestFunction()'>Template 1 Function</button>
template2.html
<button onclick='template2TestFunction()'>Template 2 Function</button>
app.js
angular.module('testApp', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('template1', {
url: '/template1',
templateUrl: 'template1.html'
})
.state('template2', {
url: '/template2',
templateUrl: 'template2.html'
});
}]);
template1Functions.js
function template1TestFunction() {
alert('Template 1 works')
};
template2Functions.js
function template2TestFunction() {
alert('Template 2 works')
};
As you can see, I have to add the template[x]functions.js file in the index.html page. I would really like to load this from the template[x].html files.