如何防止 $state 刷新任何不依赖于正在更改的 $state.var 的组件?

How to prevent $state from refreshing any Component that does not rely on the $state.var being changed?

我们正在使用 Angular-ui-router 进行应用状态管理。

我们遇到的一个问题是,当 $state 更改时,每个组件都会刷新,即使它是更新的变量,但在某些组件中没有 exist/not 使用。

例如,我们有一个 TickersPanel 和一个 TagsPanel。选择 Ticker 时,TagsPanel 应该更新和刷新,但是当在 TagsPanel 中选择标签时,TickersPanel 不应刷新,但当前会刷新。

我发现 { notify: false } 是另一个可以传入的对象。但是一旦我这样做,就不会刷新任何组件。

const save = (tag, terms) => {
    CONTAINER.push(tag);
    $state.go('charting', Term.write(terms), { notify: false }).then((stateResult) => {
        console.log('stateResult', stateResult);
    });
};

有没有人找到解决这个问题的方法?


TagsPanel $onInit(在每次 $state 更改时运行)

this.$onInit = () => {
    tagsCol.addEventListener('scroll', scrollingTags);

    this.tags = [];
    this.limit = 30;

    const panelOpen = Dashboard.panelStatus(this.tagsOpen, this.chartMax);
    panelOpen ? buildTagList() : collapsePanel();
};

TickersPanel $onInit(在每次 $state 变化时运行,如果 $state.params.ticker 没有变化,那么这不应该是 运行)

this.$onInit = () => {
    this.tickers = [];

    this.spy = {
        longname: "SPDR S&P 500",
        ticker: "SPY",
    };

    this.showTickersPanel = Dashboard.panelStatus(this.tagsOpen, this.chartMax);
    // const currentState = $state.params;
    // const prevState = Dashboard.getPreviousState();
    loadTickers().then(() => this.loadingTickersDone = true);
};

如果状态没有改变,为什么不只维护组件中的状态和 return 在函数的开头?

我已经 made a JsFiddle 描述了一种使用 uiRouter 实现我从你的问题中了解到的内容的方法。

fiddle 使用 state inheritance and named views 仅在 tickers 参数更改时重新初始化 tags 状态。

[..]
// Using @ on the view names to refer to the absolute uiViews.
$stateProvider.state('tickers', {
    url: "",
    views: {
      'tickersPanel@': {
        template: [..]
        bindToController: true,
        controllerAs: "$ctrl",
        controller: function() {
          this.$onInit = function() {
            console.info('Tickers Panel $onInit');
            this.tickers = ["Ticker1", "Ticker2", "Ticker3"]
          }
        }
      },
      [..]
    }
});
$stateProvider.state('tags', {
    parent: 'tickers',
    url: "/:ticker",
    views: {
      'tagsPanel@': {
        template: [..]
        controllerAs: '$ctrl',
        bindToController: true,
        controller: function($stateParams) {
          this.$onInit = function() {
            console.info('Tags Panel 2 $onInit');
            this.ticker = $stateParams["ticker"];
          }
        }
      }
    }
});
[..]