Leon Gaban
Leon Gaban

Reputation: 39034

How to prevent sibling state controller from refreshing?

https://plnkr.co/edit/zfmuZXp88cSbdOsibv3y?p=preview

I have a main dashboard parent state with 2 child states tags and tickers.

When I click on a button in tickers, only the tags state should refresh. Currently they both refresh.

enter image description here

enter image description here

^ The onInit tickersController console.log should only be run once. However the tagsController should be run every time a ticker is clicked.


var routerApp = angular.module('routerApp', ['ui.router']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/dash');

    var dash = {
      name: 'dash',
      url: '/dash?ticker',
      params: {
        ticker: 'AAA'
      },
      views: {
        '': { templateUrl: 'dashboard.html' },
        'tagsList@dash': {
          url: '/tags',
          templateUrl: 'tags-list.html',
          controller: 'tagsController'
        },
        'tickersList@dash': {
          url: '/tickers',
          templateUrl: 'tickers-list.html',
          controller: 'tickersController'
        },
        'alertsList@dash': {
          url: '/alerts',
          templateUrl: 'alerts-list.html',
          controller: 'alertsController'
        }
      }
    };

    $stateProvider
      .state(dash);            
});

routerApp.controller('tagsController', function($scope, $state) {
    $scope.ticker = $state.params.ticker;

    function getList(ticker) {
      switch(ticker) {
          case 'AAA' : return ['aaa tag 1', 'aaa tag 2', 'aaa tag 3'];
          case 'BBB' : return ['bbb tag 1', 'bbb tag 2', 'bbb tag 3'];
          case 'CCC' : return ['ccc tag 1', 'ccc tag 2', 'ccc tag 3'];
      } 
    }

    $scope.tags = getList($state.params.ticker);

    this.$onInit = function() {
      console.log('onInit tagsController');
    };
});

routerApp.controller('tickersController', function($scope, $state) {
    $scope.changeScotchState = function(theTicker) {
        $state.go('dash', { ticker: theTicker });
    };

    $scope.tickers = [
      'AAA', 'BBB', 'CCC'
    ];

    this.$onInit = function() {
      console.log('onInit tickersController', $state.params.ticker);
    };
});

routerApp.controller('alertsController', function($scope, $state) {

    this.$onInit = function() {
      console.log('onInit alertsController', $state.params.ticker);
    };

});

Upvotes: 0

Views: 68

Answers (2)

Stepan Shilin
Stepan Shilin

Reputation: 133

https://plnkr.co/edit/iramGsSrJ2vKYvDPWFTb?p=preview

You can't keep a view from refreshing without moving it to the one of the parent states. Here's the changed states that should work

    var tags = {
      name: 'dash.tags',
      url: '?ticker',
      views: {
        'tagsList@dash': {
          templateUrl: 'tags-list.html',
          controller: 'tagsController'
        }
      }
    }

var dash = {
  name: 'dash',
  url: '/dash',
  abstract: true,
  views: {
    '': { templateUrl: 'dashboard.html' },
    'tickersList@dash': {
      templateUrl: 'tickers-list.html',
      controller: 'tickersController'
    }
  }
};

Upvotes: 1

Dominik Ruczyński
Dominik Ruczyński

Reputation: 81

i edited Your plunker to work without state refresh:

https://plnkr.co/edit/ntGVGQ06yVQC4W0Fl7A8?p=preview

The broadcast:

    $scope.changeScotchState = function(theTicker) {
   $rootScope.$broadcast('newData',  theTicker);
};

and $on:

   $scope.$on('newData', function(event, ticker){

      $scope.tags = getList(ticker);
      $scope.ticker = ticker;
    })

are the most important part and change.

Upvotes: 1

Related Questions