Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In AngularJS, any inline javascript code that included in HTML templates doesn't work

In AngularJS, any inline javascript code that included in HTML templates doesn't work.

For Example:

main.html file:

<div ng-include="'/templates/script.html'"></div> 

And script.html file:

<script type="text/javascript">     alert('yes'); </script> 

When I open main page, I expect an alert message that say 'yes' but nothing happens. I think some security restrictions in the AngularJS is preventing inline scripts, but I couldn't find any workaround about that.

Note: I don't use jQuery or any other framework, only AngularJS 1.2.7.

like image 832
Murat Çorlu Avatar asked Jan 24 '14 15:01

Murat Çorlu


People also ask

Can we use JavaScript in AngularJS?

You need to execute a separate java-script function. For an angular application it is not a proper way to run javascript out of scope of angular. It will ensure that the external function exist before starting app. It will add more flexibility and control over the java script function in angular.

How does AngularJS work with HTML?

AngularJS extends HTML with ng-directives. The ng-app directive defines an AngularJS application. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data. The ng-bind directive binds application data to the HTML view.

What are the templates available in AngularJS?

There are two types of template: Static Template. Dynamic Templates.


2 Answers

jQlite does not support script tags. jQuery does, so the recommendation is to include jQuery if you need this functionality.

From Angular's Igor Minar in this discussion:

we looked into supporting script tags in jqlite, but what needs to be done to get a cross-browser support involves a lot of black magic. For this reason we decided that for now we are just going to recommend that users use jquery along with angular in this particular case. It doesn't make sense for us to rewrite one third of jquery to get this working in jqlite.

Here's the related github issue jqLite should create elements in same way as jQuery where Igor sums up, before closing the issue, with this:

This is too much craziness for jqlite, so we are not going to do it. Instead we are going to document that if you want have script elements in ng:include or ng:view templates, you should use jquery.

demo plunker with jquery

like image 166
KayakDave Avatar answered Sep 22 '22 03:09

KayakDave


Angular uses the $sanitize on ng-include directives which strips out scripts. A better approach for templates is to create a controller for that template.

It is better to use an individual controller for templates.

In template.html

<form role="form" ng-controller="LoginController">    <input type="text" placeholder="Email" ng-model="email">    <input type="password" placeholder="Password" ng-model="password">     <button class="btn btn-success" ng-click="login()">Sign in</button> </form> 

In the LoginController you can use whatever code you want

angular.module('myApp.controllers', []).   controller('LoginController', [function() {       alert('controller initialized');   }]) 
like image 31
Nathaniel Johnson Avatar answered Sep 24 '22 03:09

Nathaniel Johnson