4

new to Angular and JS so my jargon may be off.

Within a certain section of a page I would like to load in different templates depending on the user clicks without changing the URL path. I know how to use $routeProvider with ng-view but that requires a URL change to load the related template.

I would also like to add a back link within this specific section so the user can travel back a step.

Any ideas? I couldn't find any questions similar to mine but I may be searching with the incorrect terms. Any help or suggestions are greatly appreciated.

Regards

2
  • did u check ng-include ? Commented Sep 11, 2014 at 11:02
  • @KalhanoToressPamuditha I haven't but that looks like the solution. Thanks. Commented Sep 12, 2014 at 7:55

2 Answers 2

3

And for the back button, you would have to keep a history array with past clicks/links and push pop from that as you click and click back. The "complete" solution would look similar to:

index.html

<html ng-app="app">
...
<div ng-controller="myController">

  <button ng-click="setCurrentView('tem1')">Template 1</button>
  <button ng-click="setCurrentView('tem2')">Template 2</button>
  <button ng-click="setCurrentView('tem3')">Template 3</button>

  <div ng-include="tem1.html" ng-show="currentView==tem1"></div>
  <div ng-include="tem2.html" ng-show="currentView==tem2"></div>
  <div ng-include="tem3.html" ng-show="currentView==tem3"></div>

  <button ng-click="goBack()">Back</button>

</div>
...
</html>

app.js

angular.module('app',[]).controller('myController',['$scope',function($scope){
  $scope.currentView='tem1';
  var history=[];

  $scope.setCurrentView=function(view){
    history.push($scope.currentView);        
    $scope.currentView=view;
  }

  $scope.goBack=function(){
    $scope.currentView=history.pop();
  }
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

That's really helpful, if I had more rep I'd up-vote this. Thanks.
0

I'd suggest using ng-include. This allows you to include chunks of html code from another location. You could then use ng-show/hide to display the desired piece or ng-if if you would prefer that it is left out of the dom if not required

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.