3

I have displayed dynamic tab menu's using ng-repeat and i want to get individual url for each tab is it possible to get the url? if so how can i handle the route. i think its not possible to to set the route because of the dynamic tab content, is there any other options available?

1
  • @Ilan Frumer , this a sample plunker of my code link ,please go through it Commented Jan 9, 2014 at 6:21

1 Answer 1

3

It's just a little patch, In a real application I recommend using ng-route OR ui-router.

Add url property to tabs

$scope.tabs = [
  { url: "/tab/1" , title:"Tab 1", content:"Dynamic content 1" },
  { url: "/tab/2" , title:"Tba 2", content:"Dynamic content 2" },
  { url: "/tab/3" , title:"Tab 3", content:"Dynamic content 3" },
  { url: "/tab/4" , title:"Tab 4", content:"Dynamic content 4" }
];

I Created A directive that:

  • changes the location based on active tab ( on $watch )
  • changes the active tab based on the location ( on prelink phase )
app.directive('tabRoute',function($location,$timeout){
  return {
    scope: {
      tabRoute : "="
    },
    compile: function(){
      return {
        pre: function(scope,elm,attrs){
          scope.tabRoute.active = ($location.path() === scope.tabRoute.url);
        },
        post: function(scope,elm,attrs){
          scope.$watch('tabRoute.active',function(){
            if(scope.tabRoute.active) {
              $location.path(scope.tabRoute.url)
            }
          })          
        }
      }     
    }
  }
})

finally, the markup

<tab ng-repeat="tab in tabs" 
     heading="{{tab.title}}" 
     active="tab.active" 
     disabled="tab.disabled" 
     tab-route="tab">
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Ilan Frumer it works now :) I dont know how to implement this with "ng-route OR ui-router" if any sample code is available please share the link
@Ilan, if you want to dynamically add/remove tabs in an MDI fashion, I don't think you could use UI-Router for this. I was trying to implemented using named ui-views but UI-Router was running into race conditions not always updating the newly added ui-view element in the new tab, Have you run into something similar?

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.