0

I have an Angular app which redirects route to a particular html page. But how to include related javascript with this.

For example if i click red it will load red.html. but i need to load red.js also additionally.

   <script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl : "main.html"
    })
    .when("/red", {
        templateUrl : "red.html";
    })
    .when("/green", {
        templateUrl : "green.html"
    })
    .when("/blue", {
        templateUrl : "blue.html"
    });
});
</script>
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Karthik SWOT
  • 1,129
  • 1
  • 11
  • 15

1 Answers1

-1

You should use controller property:

<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        controller : "MainController"
        templateUrl : "main.html"
    })
    .when("/red", {
        controller : "RedController";
        templateUrl : "red.html";
    })
    .when("/green", {
        controller : "GreenController"
        templateUrl : "green.html"
    })
    .when("/blue", {
        controller : "BlueController"
        templateUrl : "blue.html"
    });
});
</script>
<script src="controllers.js"></script> // add controllers files

controllers.js

angular
  .module('myApp')
  .controller('MainController', function() {
   //your MainController code
});

angular
  .module('myApp')
  .controller('RedController', function() {
   //your RedController code
});

angular
  .module('myApp')
  .controller('GreenController', function() {
   //your GreenController code
});

angular
  .module('myApp')
  .controller('BlueController', function() {
   //your BlueController code
});
guijob
  • 4,413
  • 3
  • 20
  • 39