0

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: {
          'detail@app.shows': {
              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)

Community
  • 1
  • 1
atoth
  • 838
  • 1
  • 9
  • 23
  • 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 Feb 18 '16 at 17:15
  • 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 Feb 18 '16 at 21:58

1 Answers1

0

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 https://stackoverflow.com/a/15666049/1202630

module.service(‘ShowsService’, function(){
    // Your Stuff
});
Community
  • 1
  • 1
Plummer
  • 6,522
  • 13
  • 47
  • 75
  • 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 Feb 19 '16 at 22:34