I'm trying to include a header, using ng-include from Angular into my Jade template:
doctype html
html(lang="en")
head
meta(charset='UTF-8')
meta(name='fragment', content='!')
base(href='/')
title Node Express Angular
link(rel='stylesheet', href='//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css')
link(rel='stylesheet', href='/css/syle.css')
body(ng-app='myApp')
ng-include(src='\'/partials/header.jade\'')
div.container
div.jumbotron.text-center
h1 Home Page
p This page is a draft in progress
script(src='//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.15/angular.min.js')
script(src='//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js')
script(src='/js/app.js')
The text in header.jade is the following:
nav.navbar.navbar-inverse(role='navigation')
ul.nav.navbar-nav.navbar-header
li
a(src='/home', class='navbar-brand') Home
li
a(src='/about') About
li
a(src='/login') Login
li
a(src='/register') Register
I have tried both ng-include(src='\'/partials/header.jade\'') and div(ng-include='\'/partials/header.jade\'')
In the Chrome developer console, the first one results in <!--ng-Include: undefined --> and the second: <!-- ng-Include: '/partials/header.jade' -->
Any clues?
Is there a specific reason why you're using Angular's ng-include instead of Jade's own include mechanism?
body(ng-app='myApp')
include partials/header
Reference form the the Jade docs.
I created an Angular directive to be rendered by JADE and then compiled by Angular to the given scope.... and it works!
btw,I'm using client-side Jade for browsers, the CDN is available here
for e.g:
html:
<ng-include src="'MyPageWithJadeScript.html'"></ng-include>
MyPageWithJadeScript.html:
<jade>tag#id.class jade text</jade>
angular:
app
.directive('jade',function($compile){
return{
transclude: true,
template: '<div ng-transclude></div>',
restrict: 'E',
link: function(scope, element){
element
.after(
$compile(
jade
.render(
element.html(),
{pretty:'\t'}
)
)(scope)
)
.remove();
}
};
});
Hope you find this useful
Refer to official doc ngInclude, using the code below
div(ng-include="'partials/header.jade'")
If you want to use ngInclude directive directly, use
ng-include(src="'partials/header.jade'")
Using express, I have the file user-description.jade inside of the views/ folder and to include it in another jade file I simple use the following expression:
div
include user-description.jade
Jade docs has other samples.
Related
I have a little directive and I have troubles to make it working.
here is my directive :
angular
.module('thermofluor')
.directive('myCustomer', function() {
return {
restrict: 'E',
template: 'table_plate.html'
};
});
The template is just a paragraph with "Test" inside.
In my main HTML in call the directive with this :
<my-customer></my-customer>
but nothing appear on the screen, and in the html the directive is not replace with the directives output html
The template have the same name and is in the same folder.
What am I doing wrong ?
Thanks.
It should be templateUrl
return {
restrict: 'E',
templateUrl: 'table_plate.html'
};
If you only need the template to show, you can use ng-include instead directive:
<ng-include
src="string"
[onload="string"]
[autoscroll="string"]>
...
</ng-include>
There is also the version for any element available:
<ANY
ng-include="string"
[onload="string"]
[autoscroll="string"]>
...
</ANY>
For even more info glance on https://docs.angularjs.org/api/ng/directive/ngInclude
Under either src or ng-include put the reference to the template you wish to load (depending on what version you use). For only 1 restrict it's kinda overkill to use a directive, rest you get from this approach as well.
NOTE: in above definitions, attributes in brackets are not needed for your example.
Scenario:-
I have html page rendering as angular template/view. Now there are big number of such template.
Now these templates has div in header part that is fixed through out the templates, now is there some-way that we can define one sub-template at one place and just insert that sub-template in all pages.
So that if there are any modification to be made, I will make in sub-template and that will be reflected in all the page.
Its like UserControls or Partials that we have in ASP.Net.
I am using AgularJS
Agreed that there are ui-routing for nested hosting but currently I am not looking for that.
Try
<div ng-include="path/to/template"></div>
you can create a seperate html file as template for header if it includes complex or considerable amount of elements. create a directive for this template and use it as an attribute/customTag or include it in your html using ng-include.
app.directive('header',function(){
return {
restrict: 'E', //'E': element /'A': attribute
templateUrl : 'templates/header.html'
}
});
use as attribute
<div header></div> // as Attribute
<header></header> // as element
if your header involves lot of functioning on it then you can also create a separate controller (which will act as a child controller) and add the controller to the above tags. such as
<header ng-controller="headerController"></header>
You probably want to use a directive template. This is a very simple directive with only the template information.
Eg:
<top-header></top-header>
module.directive('topHeader', function() {
return {
templateUrl: 'path',
// append
replace: true,
// attribute restriction
restrict: 'E'
}
});
In my MEAN app, I use jade as my template engine. My problem is, when I call an angular directive, jade code is not working but html code is working. My code is given below:
index.jade
div(ng-repeat="num in addDir")
admin-collection
directive.js
var formDir = angular.module("formDirective", []);
formDir.directive('adminCollection', function() {
return {
restrict: 'E',
transclude: true,
// call jade template url
templateUrl: '../_template/_formTemplate/_adminCollection.jade'
};
});
_adminCollection.jade
h1 from _adminCollection templateUrl
If I use jade format code in _adminCollection.jade, it just show a plain text, not text inside h1 tag
But following code is working:
directive.js
var formDir = angular.module("formDirective", []);
formDir.directive('adminCollection', function() {
return {
restrict: 'E',
transclude: true,
// call jade template url
templateUrl: '../_template/_formTemplate/_adminCollection.html'
};
});
_adminCollection.html code::
<h1> from _adminCollection templateUrl </h1>
How can I solve this problem?
Jade is a template engine. Browser has only built-in html parser, so it does not understand what jade code means and treats it as plaintext.
To make it work you need to convert it to html. You can use some task manager to do it. Two most popular task managers for node.js are gulp and grunt. Each of them has a working jade plugin which you can use right away.
Jade is something like less - it must be convert to another format, because browser can't understand that. When you use less, you have to transform it to css. And if you use jade - to html.
If you use grunt, you should look on it: https://github.com/gruntjs/grunt-contrib-jade
Otherwise you can check if your IDE can transform jade to html. For example PhpStorm can do this in automatic way.
Then in your directives you should specify path to html template, no jade.
You can use following directory structure:
app/
src/
js/
less/
jade/
dist/
templates/ <-- here you can put your htmls
styles/ <-- and here put css
js/ <-- if you want, you can put this minimalized app.js
that will contain all of your project,
see grunt-contrib-uglify for more info
EDIT: here is really great article about grunt: http://anthonydel.com/my-personal-gruntfile-for-front-end-experiments/ There is much more then you need, but maybe it will help you
.... Or you can to use webpack to do the work.
then you can load the template like this:
angular.module('app').component('myComponent', {
bindings: {},
template: require('./mytemplate.jade')()
});
You can to note that I'm invoking the returned function.
Another option is to keep the HTML templates in your DOM, but hidden:
div(ng-non-bindable, style="display: none")
#adminCollectionTemplate
div(ng-repeat="num in addDir")
admin-collection
#anotherTemplate
//- Alternatively, pull in the template from another file
include ./_formTemplate/_adminCollection.jade
and then use jQuery to fetch the HTML out of the DOM, and give it to angular:
formDir.directive('adminCollection', function() {
return {
restrict: 'E',
transclude: true,
// fetch template from DOM
template: $('#adminCollectionTemplate').html()
};
});
This will just work, without any Ajax or any task runners. But it will clutter up the DOM with templates that could otherwise be hidden away in the JS. And it is an extra step to place every new template into that div.
ng-non-bindable is needed to tell Angular to leave the template elements alone (don't manipulate them or bind to them), until their clones are actually used by the directive.
I have a directive that lets me insert a chat component. It loads a template as required.
mod.directive('chat', function () {
return {
replace: true,
templateUrl: '/tmpl/chat/chat',
}
})
But the problem is that the template needs additional includes:
.chat
.chatHeader(ng-include="'chatHeader'")
.chatLog(ng-include="'chatLog'")
Partials:
script( type="text/ng-template", id='chatLog')
.chatMsg(ng-include="'chatMsg'", ng-repeat='chatMsg in chatLog')
script( type="text/ng-template", id='chatHeader')
h3 Chat
How do I load the includes? I used to put the partials in the same file as base chat template but if I use replace: true that's no longer allowed.
What's the standard angular way to load included partials?
Using ng-include as you are is perfectly valid. The issue you are facing is likely because your template partial does not have exactly 1 root element. Here is a plunk with a working version of what you are describing: http://plnkr.co/wy1a3T1UdKOlBQXr9o22
I am using the Angular UI bootstrap module for opening a dialog.
There is a option for mentioning the path of template like
$scope.opts = {
backdrop: true,
keyboard: true,
backdropClick: true,
template: '<p>Template</p>',
controller: 'TestDialogController'
}
my app is in site/app then js , partials
I am not able to find how it finds templates
The Angular UI bootstrap dialog takes either a template inline,
template: '<p>This is a inline template</p>';
or the template url can be specified as both a relative and absolute path.
templateUrl: '/relative/path/dialog.html';
Try to open a browser tab pointing to your template first, and when you successfully have derived the path you can try to add it to your templateUrl.
You can read more about correct usage of the directive here.
There's a use of <script> as a directive which would allow you to make a hybrid approach.
Angular docs mention that if you use a declaration in your html like:
<script type="text/ng-template" id="/tpl.html">
Content of the template.
</script>
Then you can use it as a reference in a directive such as ngInclude or ngView, and even a service like $route. You can see this working in this fiddle.