我们正在使用 Angular-ui-router 用于我们的应用程序状态管理。
我们遇到的一个问题是,当 $state
更改时,每个组件都会刷新,即使它是一个在某些组件中不存在/未使用的更新变量。
例如,我们有一个 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);
};
最佳答案
我已经 made a JsFiddle描述一种使用 uiRouter 实现我从你的问题中理解的方法。
fiddle 使用 state inheritance和 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"];
}
}
}
}
});
[..]
关于javascript - 如何防止 $state 刷新任何不依赖于正在更改的 $state.var 的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254616/