0

I just followed the tutorial on the link below https://scotch.io/tutorials/single-page-apps-with-angularjs-routing-and-templating

I did exactly as is described on the page, but evoluting it to a real project, I had to put some javascript code inside internal pages, pages/about.html for example. and for some reason, it is not executed. even you put a single

<script>
    alert('foo');
</script>

it's not interpreted as a javascript code. and the alert is not shown

pages are injected using ng-view

<div id="main">

    <div ng-view></div>

</div>

whole code can be viewed in https://scotch.io/tutorials/single-page-apps-with-angularjs-routing-and-templating

thanks

3
  • 1
    check out this answer stackoverflow.com/questions/21336350/… Commented Sep 22, 2015 at 22:00
  • My guess would be the html is being inserted with innerHTML, which doesn't execute javascript. jsfiddle.net/rc4yv1Lq/1 Commented Sep 22, 2015 at 22:00
  • it's really easy to put code into directives. Will find it a much simpler path if you are just starting out Commented Sep 22, 2015 at 22:29

1 Answer 1

0

I´ve been using scripts inside my templates (for lazy loading) for some time now without knowing that AngularJS wasn't doing the entire work. However, you can acomplish the task if you combine AngularJS with JQuery.

Check the following snippet for a solution:

index.html

<!DOCTYPE html>
<html ng-app="app">

<head>

  <meta charset="utf-8">

  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-route.js"></script>

</head>

<body>

  <h1>angular app</h1>

  <div ng-view></div>

  <script type="text/javascript">
    var app = angular.module('app', ['ngRoute']);

    app.config(function($routeProvider) {

      $routeProvider

        .when('/', {
        template: '<a href="#script">i´m feeling lucky!</a>'
      })

      .when('/script', {
        templateUrl: 'script.html'
      });

    });
  </script>

</body>

</html>

script.html

script successfully loaded!
<script>alert('ok');</script>

The import order is important in this case. If you swap the JQuery with the AngularJS the solution will not work.

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.