gpt4 book ai didi

javascript - 如何防止特定 $state 在另一个 $state 更新时刷新

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:20 29 4
gpt4 key购买 nike

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

预期

登录后,所有 $states 都会初始化,然后单击 Ticker 按钮后,唯一应重新初始化的 $states 是 tickertags社交,但不是动态

结果

登录后,所有 $states 都会初始化,然后单击 Ticker 按钮后,所有 $states 都会重新初始化。 提要不应该。

enter image description here

应该如何构建仪表板和提要模块来防止这种情况发生?目前我有 <feed-module></feed-module>里面dashboard.html 。它和仪表板是否应该在另一个中间状态/组件中进一步分离?也许是一个container.component?

完整代码

// Feed module
////////////////////////////////////////////////////////////////////////////////
var feed = angular.module('feed', ['ui.router'])

feed.config(function($stateProvider) {

const feed = {
name: 'feed',
url: '/feed',
templateUrl: '<em>Feed items go here.</em>'
}

$stateProvider.state(feed);

})

feed.component('feedModule', {
templateUrl: 'feed-module-template.html',
controller: function($scope, $state) {
console.log('Feed init (only once)', $state.params);
}
})

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

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

$urlRouterProvider.otherwise('/login');

const login = {
name: 'login',
url: '/login',
templateUrl: 'login.html',
bindToController: true,
controllerAs: 'l',
controller: function($state) {
this.login = function() {
$state.go('dashboard', {});
}
}
}

const dashboard = {
name: 'dashboard',
url: '/dashboard',
params: {
ticker: {},
tags: {}
},
views: {
'' : {
templateUrl: 'dashboard.html',
},
'tickers@dashboard': {
templateUrl: 'tickers-module-template.html',
controller: function($scope, $state) {
console.log('Tickers init', $state.params);

$scope.tickers = [
{ id: 1, ticker: 'AAPL' },
{ id: 2, ticker: 'GOOG' },
{ id: 3, ticker: 'TWTR' }
];

$scope.clickTicker = function(ticker) {
console.log(' ')
console.log('Ticker clicked!')
$state.go('dashboard', { ticker: ticker });
}
}
},
'tags@dashboard' : {
templateUrl: 'tags-module-template.html',
controller: function($scope, $state) {
const tags_model = [
{
ticker: 'AAPL',
tags : [{ id: 1, term: 'iPhone 7' }, { id: 2, term: 'iPhone 8' }, { id: 3, term: 'Tim Cook' }]
},
{
ticker: 'GOOG',
tags : [{ id: 4, term: 'Pixel' }, { id: 5, term: 'Pixel XL' }, { id: 6, term: 'Chrome Book' }]
},
{
ticker: 'TWTR',
tags : [{ id: 7, term: 'tweet' }, { id: 8, term: 'retweet' }, { id: 9, term: 'moments' }]
}
];

function matchTags(ticker, model) {
return model.filter(function(obj){
if (obj.ticker === ticker) { return obj; }
});
}

$scope.tags_model = matchTags($state.params.ticker.ticker, tags_model)[0];

$scope.clickTag = function(tag) {
$state.go('tags', { tag: tag });
}

console.log('Tags init', $state.params);
// console.log(' Tags model', tags_model);
}
},
'social@dashboard' : {
templateUrl: 'social-module-template.html',
controller: function($state) {
console.log('Social init', $state.params);
}
}
}
}

$stateProvider
.state(login)
.state(dashboard);
});

想一想,这样的架构行得通吗?基本上login将导航到 container state 将包含 2 个组件( dashboardfeed ),每个组件都有自己的内部状态?

enter image description here

最佳答案

我可以根据您所在的州来解决此问题。基于您只希望在加载仪表板时加载提要“一次”的事实,我正在检查先前状态的名称,如果它不是仪表板,那么我才会继续执行其余部分 Controller 代码。这是笨蛋。请注意,如果添加更多状态,这不是一种可扩展的方法,并且如果其他人有更好的方法,我真的很希望看到这一点。

我更改了 Controller 代码如下:

feed.component('feedModule', {
templateUrl: 'feed-module-template.html',
controller: function($scope, $state) {
var previousState = $state.params.previousState.name;
if(previousState !== 'dashboard') {
console.log('Feed init (only once)', $state.params); //do whatever u want here
}
}
});

我将以前的状态作为参数传递给其他状态,如下所示:

controller: function($state) {
this.login = function() {
$state.go('dashboard', {previousState : { name : $state.current.name }})
}
}

看看完整的骗子here看看这是否对您的情况有帮助!

关于javascript - 如何防止特定 $state 在另一个 $state 更新时刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42844302/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com