0

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.

3 Answers 3

6

Try

<div ng-include="path/to/template"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is simplest, and most directly does exactly the thing asked for. The other answers are probably more suitable for a real application, because quite commonly over time it becomes desirable to add some behavior as well as just a reused template. But that is more than needs to be understood for the problem at hand.
1

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>

Comments

0

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'

    } 
});

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.