2

I used ngRoute to load html

var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $routeProvider
  .when('/route1', {
    templateUrl: '/view1',
    controller: 'controller1'
  })
  .when('/route2', {
    templateUrl: '/view2',
    controller: 'controller2'
  })
}]);

When I switch to route1, I get html like this:

<div>
  bla bla bla ...
</div>
<script src="js1.js"></script>

Then how to load js1.js after getting html successfully?

worldask
  • 1,837
  • 3
  • 22
  • 37
  • could you please explain it clearly. – gunas Sep 16 '14 at 11:25
  • possible duplicate: [execute-javascript-loaded-via-ajax](http://stackoverflow.com/questions/12021095/execute-javascript-loaded-via-ajax) – Adrian Forsius Sep 16 '14 at 11:28
  • Sorry for my wrong description. I've edited my question. – worldask Sep 16 '14 at 11:57
  • If the problem still is loading javascript dynamically with ajax, then it is the same issue did you see watch the link? maybe this is easier: [how-do-you-execute-a-dynamically-loaded-javascript-block](http://stackoverflow.com/questions/75943/how-do-you-execute-a-dynamically-loaded-javascript-block) – Adrian Forsius Sep 16 '14 at 12:08
  • er...I don't know where to put codes to run `eval`, because `ng-route` encapsuled the ajax work. – worldask Sep 16 '14 at 12:18

1 Answers1

1

I know that is an old question, but I had the same problem and, searching on Google, I've solved. You can use this (with jQuery): $.getScript("js1.js", function(){}); in your controller1, like this:

app.controller('controller1', function(/*parameters you need*/){
  /*your controller code*/
  $.getScript("js1.js", function(){});
});

You can see other about $.getScript here: http://api.jquery.com/jquery.getscript/

and in your template1 don't put js.

Simone Sessa
  • 795
  • 1
  • 12
  • 35