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

The goal

We have a number of views which we want to fill with the same template/controller. Each of them should receive an id which will be loaded without affecting other controllers. We don't want to use the scope: { item: '=' } two way binding in the .directive of the template. Think of thousands of elements loaded this way (in the example we are only having two elements).

What we tried

We started using multiple views/nested views to do the rescue. We ended up with a version where many views are loading the same component, but all loading the same id, which is not the expected behavior as we want to control each of them separately, i.e. in this example clicking on title and passing its id to content1 should not cause content2 to load it as well.

(The code is based on this sample: http://www.funnyant.com/angularjs-ui-router/)

Short code

index.html

<div ui-view="header"></div>
<div ui-view="content1"></div>
<div ui-view="content2"></div>
<div ui-view="footer"></div>

script.js

  .state('app.shows', {
      url: 'shows',
      views: {
          'content1@': {
              templateUrl: 'shows.html',
              controller: function($scope, ShowsService) {
                $scope.shows = ShowsService.list();
              }
          },
          'content2@': {
              templateUrl: 'shows.html',
              controller: function($scope, ShowsService) {
                $scope.shows = ShowsService.list();
              }
          }
      }
  })
  .state('app.shows.detail', {
      url: '/',
      params: {
        // this is for hiding parameter from URL
        id: null,
      },
      views: {
          '[email protected]': {
              templateUrl: 'show-detail.html',
              controller: function($scope, $stateParams, ShowsService) {
                  $scope.selected = ShowsService.find($stateParams.id);
                }
          }
      }

  });

Plunker example

http://plnkr.co/edit/Dq5sDBzREqDzlEnNVhfp?p=preview

The question

What should we do to prevent the other view (e.g. content2 when I click on a content1 title) to change at the same time? Also in production we will have many views, not just these two, but we simplified the example for sake of brevity.

Right now we are not sure if we are missing something from the documentation or the concept is wrong and we want to abuse ui router for something it was not designed for.

(Similar question we are aware of but do not answer the question where there are multiple views loading the same component on the same screen: angular ui router state - multiple states with same template and controller)

share|improve this question
1  
What is the code not doing that you expect it to do? That part of your question isn't clearly defined. From what I can surmise, you're attempting an ng-repeat of a particular directive managed through a route? – Plummer 2 days ago
    
I edited the question. I hope it's now clear. In short: changes in content1 should not change content2. Right now this is how it works. – atoth 2 days ago

Without seeing what ShowsService.list(); looks like, it's kind of hard to diagnose.

My guess is `ShowsService is registered as a factory over a service. See this answer http://stackoverflow.com/a/15666049/1202630

module.service(‘ShowsService’, function(){
    // Your Stuff
});
share|improve this answer
    
You don't need to guess. It is in the plunker example, script.js from line 69. Yes, it is registered as a factory. – atoth yesterday

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.