Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

In my app I have a slidebox style interface. In one of the slides, I want to show a certain thing conditionally (ie if Meteor.user().profile.asdf). I want to show the exact same page via navigation from a link on the main page (so if you're at the URL /item/123). How can I do this without duplicating all of my code? More specifically, in my router I have:

.state('item', {
  url: '/item/:itemId',
  templateUrl: 'client/templates/itemView.html',
  controller: 'itemViewCtrl as item',
  resolve : {
    liftId : ['$stateParams', function($stateParams){
      // console.log($stateParams.liftId);
      return $stateParams.liftId;
    }]
  }
})

On the page accessed via the URL I access the data through item.helper.whatever. Is there a shortcut to include this page in the main page as an <ng-include src="'client/templates/itemView.html'"></ng-include> or something?

share|improve this question

Angular should do that automatically if the templateUrl is the same for both components. Angular uses a $templateCache. Reference

When angular goes to fetch you URL it will check the $templateCache first and will realize the template is from the same place and not fetch the static content again. Instead it will use the one stored in $templateCache


You can validate this by looking at your network traffic in developer tools. Only one request for that url should be sent.

If it is not, or you want to make sure you are using the same exact template another approach is to manually add a template to the cache using.

$templateCache.put('templateId.html', 'This is the content of the template');

Then instead of using templateUrl for your State you would use template

share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.