【发布时间】:2017-03-16 20:36:04
【问题描述】:
https://plnkr.co/edit/bOZW1a9u62W1QA6cYjYj?p=preview
预期
在Login所有$states初始化之后,然后在点击Ticker按钮后,唯一应该重新初始化的$states是ticker、tags 和 社交,但不是 feed。
结果
在 Login 后所有 $states 初始化,然后在单击 Ticker 按钮后所有 $states 重新初始化。 Feed 不应该。
应如何构建仪表板和提要模块以防止这种情况发生?目前我在dashboard.html 中有<feed-module></feed-module>。它和仪表板是否应该在另一个中间状态/组件中进一步分离?一个 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 状态,该状态将包含两个组件(dashboard 和feed),每个组件都有自己的内部状态?
【问题讨论】:
-
我认为这是不可能的。通知所有、不通知或重新加载当前状态(带/不带通知)都可以。
标签: javascript angularjs angular-ui-router state state-machine