2

I'm using ng-inculde to include an html file that aslo includes some javascript codes for that page, but the javascripts are not executed,

here is my Code :

    <head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title ng-cloak>{{Page.Title}}</title>
  <link rel="stylesheet" href="/media/css/bootstrap.min.css">
  <link rel="stylesheet" href="/media/css/swiper.min.css">
  <link rel="stylesheet" href="/media/css/owl.carousel.css">
  <link rel="stylesheet" href="/media/css/style.css">
  <link href="/media/css/nprogress.css" rel="stylesheet">
  <script type="text/javascript" src="/media/js/angular.js"></script>
  <script src="/media/js/jquery-1.11.2.min.js"></script>
  <script type="text/javascript" src="/media/js/app.js"></script>
  <script src="/media/js/vendor/modernizr-2.8.3.min.js"></script>
  <meta name="description" content="">
  <meta name="fragment" content="!">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script>document.write('<base href="' + document.location + '" />');</script>
  <link rel="apple-touch-icon" href="apple-touch-icon.png">

  <script type="text/javascript" src="/media/js/ng-device-detector.js"></script>
  <script type="text/javascript" src="/media/js/re-tree.js"></script>
  <script type="text/javascript" src="/media/js/angular-route.js"></script>
  <script type="text/javascript" src="/media/js/angular-md5.js"></script>
  <script type="text/javascript" src="/media/js/angular-cache.js"></script>
  <script type="text/javascript" src="/media/js/bootstrap.min.js"></script>
</head>
<body>
      <div ng-view></div>
</body>
</html>

And my home.html page:

<p>Welcome, this is the home page!!</p>
<div ng-include src="'/template/include/home_s.html'"></div>

And this is my home_s.html:

<script>
  console.log("Selam, chooni khasi?");
  alert("Sellam, choooni?");
</script>
4
  • Try with <div ng-include="/template/include/home_s.html"></div>. Commented Nov 15, 2015 at 7:02
  • I've tried that too! didn't help Commented Nov 15, 2015 at 7:06
  • Your javascript files are loaded using relative references. Is it possible that the relative references are breaking because the include is from a relative subfolder of home.html? Have you confirmed that the javascript files load correctly in the browser? Commented Nov 15, 2015 at 8:37
  • Yes, javascript file is loaded correctly, but is not running!! why is that? Commented Nov 16, 2015 at 7:05

1 Answer 1

2

Either:

<ng-include src="'/template/include/home_s.html'"></ng-include>

Or:

<div ng-include="'/template/include/home_s.html'"></div>

However, Angular on its own doesn't support loading scripts inside templates. For this to work you need to include jQuery (make sure you load it before Angular).

Demo without jQuery: http://plnkr.co/edit/Qh27ArEyIk53VRB7e61C?p=preview

Demo with jQuery: http://plnkr.co/edit/TxOmx5ksEFC2larJiJBA?p=preview

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.