Loading AngularJS combined directive templates with NgInclude - javascript

I want to include all of my directive templates within a single file to reduce the number of HTTP requests required to load a directive heavy page.
I have this directive
angular.module('bwDirectives', [])
.directive('bwInfoCard', function () {
return {
restrict: 'E',
transclude: false,
replace: true,
scope: { title: '=' },
templateUrl: "one-template",
};
})
If I specify the templates in-line like this then it loads the directive template properly:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Directive Test Fixture</title>
<link rel="stylesheet" href="../Style.css" type="text/css" />
<script src="../../../Libraries/angular.min.js"></script>
<script src="../Widget.js"></script>
<script src="./Fixture.js"></script>
</head>
<body ng-app="BaseWidgetFixtures">
<h1>Base Info Card</h1>
<script type="text/ng-template" id="one-template">
<div>This is first template</div>
</script>
<script type="text/ng-template" id="two-template">
<div>This is second template</div>
</script>
<div ng-controller="InfoCardFixture">
<bw-info-card title="title"></bw-info-card>
</div>
</body>
</html>
If I try to include the templates via NgInclude it however fails. I guess it tries to load the template for the directives before doing NgInclude (even though it is earlier in the file). The correct script tags are getting included on the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Directive Test Fixture</title>
<link rel="stylesheet" href="../Style.css" type="text/css" />
<script src="../../../Libraries/angular.min.js"></script>
<script src="../Widget.js"></script>
<script src="./Fixture.js"></script>
</head>
<body ng-app="BaseWidgetFixtures">
<h1>Base Info Card</h1>
<div ng-include src="'templates.html'"></div>
<div ng-controller="InfoCardFixture">
<bw-info-card title="title"></bw-info-card>
</div>
</body>
</html>
Is there something that I am missing or can you not NgInclude templates for use with directives?
Thanks.

Take a look at this discussing where Pete Bacon Darwin explains the order that directives are linked/compiled. https://groups.google.com/forum/#!topic/angular/3HsNz4ncnYA/discussion. You could probably rearange your html to get this working but this is not really what ng-include was meant to do. I'd recommend using something like angular-templatecache if you don't want to load your templates with AJAX.

Related

Displaying AngularJS content within Views

When I use the following static HTML with views, everything works as I expect it would, shown here:
https://embed.plnkr.co/MJYSP01mz5hMZHlNo2HC/
Now what I am asking, is how do I properly get this angular-ui accordion working properly within the ng-view?
See the accordion I am trying to use here:
http://plnkr.co/edit/1ZWBsbO6sQRnZ7CpPrdz?p=info
And my test plunker here, showing that the HTML shows up in the view, but it lacks JS functionality:
https://plnkr.co/edit/I3myvfH5KUkpfV1aWh72?p=info
<!DOCTYPE html>
<html>
<head>
<script data-require="angularjs#1.5.7" data-semver="1.5.7" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<script data-require="angular-route#1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular-route.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.3.js"></script>
<script src="script.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body ng-app="Test">
<div ng-include='"header.html"'></div>
<div ng-view></div>
<div ng-include='"footer.html"'></div>
</body>
</html>
You have to define a controller with the name(PageCtrl) you defined.
app.controller('PageCtrl', [function(){
// Your business logic
alert("In am controller");
}])

.js not getting applied on ng-view

I have the index.html with ng-view inside. I have imported respective .js file to the index.html but those .js not getting applied on injected view.
index.html
<!DOCTYPE html>
<html class="no-js css-menubar" lang="en" >
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<head>
<title>gg</title>
<!-- Scripts -->
<script src="js/userdat.js"></script>
</head>
<body class="dashboard site-menubar-push" ng-app="app">
<h1>Header</h1>
<ng-view></ng-view>
<script src="assets/js/jquery-1.9.1.js"></script>
<script src="assets/js/jquery.mobile-1.4.2.js"></script>
<!-- AngularJS Angular-route -->
<!--Angular linking -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-route.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-resource.js">
</script>
</body>
</html>
The line <script src="js/userdat.js"></script> has jQuery stuff and it loads perfectly but the logic return in it not working on ng-view injected ui elements.if i import it in partials it works fine but not stable, Do you know How can I import it properly.
partials.html
<script src="js/userdat.js"></script>
<div class="jumbotron text-center">
<h1>Home Page 4 Life</h1>
<p>hi</p>
</div>
Either place all the CDN includes (jquery, angular, ...) in the <head> element or place the the userdat.js at the bottom of the <body>.
Note that you if you're accessing DOM elements with JQuery that are injected into the ng-view, you'll need to listen to $routeChangeSuccess events because the html is modified after an angular digest.
The best way to do this would be using RequireJS, but it's going to be pretty tough if your project is already big.
With RequireJS you could even add a resolve to the route:
resolve: {
userdat: function($q) {
var deferred = $q.defer();
require(['./js/userdat.js'], function(userdat) {
deferred.resolve(userdat);
});
return deferred;
}
}
If implementing RequireJS is too hard, I would just hardcode the code into the view.

script not running in templateurl

This is my angular js file
test.js file:
var app = angular.module("angleapp", [])
.controller('MainController', ['$scope', function ($scope) {
$scope.test = "hi all"
}])
.directive('programlisting', function () {
return {
restrict: 'E',
scope: {
},
templateUrl: '/directives/test.html'
};
});
test.html file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
alert("stop");
</script>
</head>
<body>
hi how are you
</body>
</html>
This is my index file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script src="/directives/test.js"></script>
</head>
<body data-ng-app="angleapp">
<div class="main" ng-controller="MainController">
<programlisting></programlisting>
</div>
</body>
</html>
Now when I try to run this
hi how are you
is coming in the output. but the alert window does not pop-op where i am i going wrong?
The problem is that script tags are not executed by browser automatically when injected with innerHTML (what happens in case of templateUrl). Normally this tags are evaluated programmatically (eval). jQuery does it but jqLite (internal jQuery-like implementation of Angular) does not.
In your case the fix is simple, move jquery script tag before angular, then Angular will use jQuery for DOM manipulations:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
Also, you need to clean up your test.html template, leave only body content. This is how test.html should look:
hi how are you
<script type="text/javascript">
alert("stop");
</script>
And finally, I don't know why you need to have script tag inside of partial template, but in most cases this is bad idea, and there are better approaches.
In partials html page(test.html), No need to write doctype, head and body tags. Just write your html elements and script. It should work.

ng-include causes angular issues

I am having some problems using ng-include. At first I was trying to use it in my project but it caused all the content to repeat indefinitely. Figuring it was something with the way I wired the project I started over. I haven't really added anything except for the angular file and one include and it keeps causing Angular to throw "WARNING: Tried to load angular more than once." and then it times out. No idea what is causing this.
<!DOCTYPE html>
<html lang="en" ng-app>
<head>
<meta charset="UTF-8">
<title>Website</title>
<!-- FOR ANGULAR ROUTING -->
<base href="/">
<!-- CSS Files-->
<!-- Vendor CSS-->
<link rel="stylesheet" type="text/css" href="../libs/bootstrap/dist/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="../libs/font-awesome/css/font-awesome.css"/>
<link rel="stylesheet" href="../css/main.css">
</head>
<body>
<div ng-include src="'navigation.html'"></div>
<!-- JS Files -->
<!-- Vendor JS-->
<script type="text/javascript" src="../libs/angular/angular.js"></script>
</body>
</html>
Ultimately what I want is something like this:
<html ng-app="app">
<head>
</head>
<body>
<div ng-include src="'navigation.html'"></div>
<div ng-include src="'header.html'"></div>
<div ui-view></div>
<div ng-include src="'header.html'"></div>
<script src="../libs/angular/angular.js"></script>
</body>
</html>
Plunker:
http://plnkr.co/edit/kFXFKC0VR5OIxwcpZFue?p=info
While not a true answer, after looking back at this, it is way easier to accomplish this with directives.
even just
.directive('myDirective', function() {
return {
template: '<h1> Hello World</h1>'
};
});
then
<my-directive></my-directive>

My "Hello World" Angular JS is not working

I have been following a tutorial to learn Angular js, and i am using Web Storm 9.0.1.
I have simply created a Html page and loaded the Angular.js file to the project.
this is my simple code:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
<script src="angular.min.js"></script>
<script type="text/javascript">
function HelloWorldCtrl($scope) {
$scope.helloMessage = "Hello World ";
}
</script>
</body>
</html>
But all i get when i run the page is :{{helloMessage}} as a header 1 !
What should i do ? what i am missing here ?
You need:
<body ng-app>
This ngApp is needed to make understand AngularJS where it has to work
Here's your the working code:
http://plnkr.co/edit/lPOknrPD5dykwImL6Pb9?p=preview
You just haven't initialized the Angular application. All you need to do is modify your body tag a bit. Notice the ng-app attribute in the body tag below:
function HelloWorldCtrl($scope) {
$scope.helloMessage = "Hello World ";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</body>
check Angular hello word in plunker
<html ng-app="plunker">
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
</body>
</html>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
You always need to put the directive first. You can also do it in or any other html tag. The ng-app directive tells the browser 'hey, this is an Angular app, let's see the script'. After the ng-app it will recognize all Angular directives.
It's also better practice to put the angular script loading at the bottom of the html, after the body. It's advised to load it last for performance and screen loading reasoning.
Thanks all, Yes indeed the problem was with the missing ng-app directive.
Note: the tutorial i was following missed this point!
A working Code:
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="css/default.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</header>
<section>
</section>
<footer>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script type="text/javascript">
function HelloWorldCtrl($scope) {
$scope.helloMessage = "Hello World";
}
</script>
</body>
</html>
Missing the ng-app directive:
<body ng-app="">...</body>
Try this code if you have not found any solution yet.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title> Hello World </title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
</head>
<body>
<div ng-app="HelloWorldApp">
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
</div>
<script>
// INITIALIZE THE APP.
var hello = angular.module('HelloWorldApp', []);
// ADD THE CONTROLLER.
hello.controller(
'HelloWorldCtrl',
['$scope', function ($greet) {
$greet.helloMessage = 'Hello World!';
} ]
);
</script>
</body>
</html>
I have defined the "ng-app" directive inside the DIV element and later initialized it in the script. This way I can add multiple view and controller inside the DIV element.
For example. Add another DIV below the header tag.
<h1 ng-controller="HelloWorldCtrl">{{helloMessage}}</h1>
<div ng-controller="greet">
<span> {{greetings}} </span>
</div>
Later add this code inside the script tag.
// ADD THE SECOND CONTROLLER.
hello.controller(
'greet',
['$scope', function ($greet1) {
$greet1.greetings = 'Good Morning';
} ]
);
All you need to do is to define an AngularJS application on your page. The ng-app directive defines an AngularJS application. You can do it on your page like :
<html ng-app=""> or
<body ng-app=""> or
<div ng-app="">
Make sure you do it before you start writing other angular JS code.
For more on the fundamentals of Angular JS :
http://www.w3schools.com/angular/angular_intro.asp
change you angular version to 1.2, I did it and it worked.
here it is:
https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js

Categories

Resources