1

Using Angularjs UI-Router I'm trying to build a typical page with a main view and a side refine/filter results view.

I've forked the sample app and now modifying it to have a parent abstract state that displays the filters view and the main view so that I can share the data that the abstract state resolves.

The problem is I cannot get the filters view to appear without breaking the scope inheritance. If I add the 'views' param it breaks the scope so how can I get this to work?

Here's my code and Plunker

$stateProvider
  .state('applications', {
      abstract: true,
      url: '/applications',
      templateUrl: 'planning.html',
      controller: function($scope){
          $scope.planning = [{ id:0, name: "Alice" }, { id:1, name: "Bob" }];

          $scope.filterPlanning = function(data) {
            var output = $scope.planning;
            // test filter
            output = $filter('filter')({ name: "Alice" });
            return output;
          }
      },
       /* this breaks the scope from being inherited by the child views
      views: {
          '': {
            templateUrl: 'applications.html'
          },
          'filters@applications': {
            templateUrl: 'applications.filters.html'
          }
      },*/
      onEnter: function(){
        console.log("enter applications");
      }
  })

child states:

  .state('applications.list', {
      url: '/list',
      // loaded into ui-view of parent's template
      templateUrl: 'planning.list.html',
      onEnter: function(){
        console.log("enter applications.list");
      }
  })
  .state('applications.map', {
      url: '/map',
      // loaded into ui-view of parent's template
      templateUrl: 'planning.map.html',
      onEnter: function(){
        console.log("enter applications.map");
      }
  })

applications.html

<div class="row">
  <div class="span4">
    <div ui-view="filters"></div>
  </div>
  <div class="span8">
    <div ui-view></div>
  </div>
</div>

I already have the list, map and filters views built as directives so I'm hoping once I can get this demo working I can swap them in relatively easily.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
DAC84
  • 1,254
  • 1
  • 20
  • 30

1 Answers1

1

There is updated plunker

I would say, that the issue here is:

Any controller belongs to view not to state

so this is the adjusted code:

.state('applications', {
    abstract: true,
    url: '/applications',
    // templateUrl and controller here are SKIPPED
    // not used
    // templateUrl: 'applications.html',
    // controller: ...
    views: {
      '': { 
        templateUrl: 'applications.html',
        controller: function($scope, $filter){
            $scope.planning = [
              { id:0, name: "Alice" }, 
              { id:1, name: "Bob" }
            ];

            $scope.filterPlanning = function(data) {
              var output = $scope.planning;
              // test filter
              output = $filter('filter')({ name: "Alice" });
              return output;
            }
        },
      },
      'filters@applications': {
        templateUrl: 'applications.filters.html'
      }
    },

Check the doc:

Views override state's template properties

If you define a views object, your state's templateUrl, template and templateProvider will be ignored. So in the case that you need a parent layout of these views, you can define an abstract state that contains a template, and a child state under the layout state that contains the 'views' object.

The plunker to check it in action

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • That's perfectly explained, thanks. This is slotting into an existing site template and looking at it I cannot just have a ui-view for the whole main content area as there's other content around it. Is there a way to use two directives that are linked in some way so that they all share the same data/scope? See my [updated Plunker](http://plnkr.co/edit/FTu6mY?p=preview) for my HTML markup. – DAC84 Oct 22 '15 at 18:54
  • Have to go now, could come to you later... but from the comment not sure what is the reall issue now. Which directives? Maybe... Could you please, extend your question... if I will know, I'd help (later). But maybe also - issue new question to get larger and fresh audience – Radim Köhler Oct 22 '15 at 19:05
  • If you could come back later I would really appreciate your answer. I've simplified the problem and posted a new question so hopefully you'll get a chance to take a look: http://stackoverflow.com/questions/33289612/switching-views-within-the-same-controller-whilst-retaining-scope-data – DAC84 Oct 22 '15 at 19:59
  • I tried to show you how to http://stackoverflow.com/a/33295159/1679310 ... if this is kind of what you want, but you'd need more .. let me know. If I could I am ready to assist (with some time limitations.. but ;) – Radim Köhler Oct 23 '15 at 07:56
  • 1
    Thank you Radim for your informative answers. With your two answers and someone else's I managed to get it working. I haven't used `ui-router` but rather `ngRoute` due to time constraints on the project so maybe I'll revisit it to refactor the code when time allows. – DAC84 Oct 23 '15 at 15:45