https://plnkr.co/edit/nqyBTcBgBimjkrpf2oYo?p=preview
Expected
After Login Selecting a Ticker button should make the Tags module display the matching Tags for that Ticker.
Results
After Login Selecting a Ticker button will replace the entire app in the index's ui-view with the Tags $state object.
App's current state path: -> login > container > tags
The ticker button click in the Tickers component:
$scope.clickTicker = function(ticker) {
console.log(' Ticker clicked!', ticker)
$state.go('tags', { ticker: ticker });
}
app.container.html (We go here after login) container state
<div>
<dashboard-module></dashboard-module>
<feed-module></feed-module>
</div>
dashboard.html dashboard state
<div class="jumbotron text-center">
<h1>The Dashboard</h1>
</div>
<div class="row">
<tickers-module></tickers-module>
<tags-module></tags-module>
<view-module></view-module>
<social-module></social-module>
</div>
Full Code
// Container module
////////////////////////////////////////////////////////////////////////////////
var container = angular.module('container', [ 'ui.router' ])
container.config(function($stateProvider) {
const container = {
name: 'container',
url: '/container',
templateUrl: 'app.container.html'
}
$stateProvider.state(container);
});
// Tickers module
////////////////////////////////////////////////////////////////////////////////
var tickers = angular.module('tickers', ['ui.router'])
tickers.config(function($stateProvider) {
const tickers = {
name: 'tickers',
url: '/tickers',
params: {
ticker: {}
},
// parent: 'dashboard',
templateUrl: '<p>Tickers State</p>',
controller: function($scope, $state) {
console.log('Tickers state', $state.params);
}
}
$stateProvider.state(tickers);
})
tickers.component('tickersModule', {
templateUrl: 'tickers-module-template.html',
controller: function($scope, $state) {
console.log('Tickers component', $state.params);
$scope.tickers = [
{ id: 1, ticker: 'AAPL' },
{ id: 2, ticker: 'GOOG' },
{ id: 3, ticker: 'TWTR' }
];
$scope.clickTicker = function(ticker) {
console.log(' Ticker clicked!', ticker)
$state.go('tags', { ticker: ticker });
}
}
});
// Tags module
////////////////////////////////////////////////////////////////////////////////
var tags = angular.module('tags', ['ui.router'])
tags.config(function($stateProvider) {
const tags = {
name: 'tags',
url: '/tags',
params: {
ticker: {}
},
// parent: 'dashboard',
template: '<p>Tags State</p>',
controller: function($scope, $state) {
console.log('Tags state', $state.params);
}
}
$stateProvider.state(tags);
})
tags.component('tagsModule', {
templateUrl: 'tags-module-template.html',
controller: function($scope, $state) {
console.log('Tags component', $state.params);
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);
}
});
// ViewHeader module
////////////////////////////////////////////////////////////////////////////////
var view = angular.module('view', ['ui.router'])
view.config(function($stateProvider) {
const view = {
name: 'view',
url: '/view',
params: {
tag: {}
},
template: '<p>View state</p>',
}
$stateProvider.state(view);
})
view.component('viewModule', {
templateUrl: 'view-module-template.html',
controller: function($scope, $state) {
console.log('View component', $state.params);
$scope.tag = $state.params.tag;
}
});
// Social module
////////////////////////////////////////////////////////////////////////////////
var social = angular.module('social', ['ui.router'])
social.config(function($stateProvider) {
const social = {
name: 'social',
url: '/social',
params: {
tag: {}
},
template: '<p>Social state</p>',
}
$stateProvider.state(social);
})
social.component('socialModule', {
templateUrl: 'social-module-template.html',
controller: function($scope, $state) {
console.log('Social component', $state.params);
$scope.tag = $state.params.tag;
}
});
// 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', 'container', 'tickers', 'tags', 'view', 'social', '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('container', {});
}
}
}
const dashboard = {
name: 'dashboard',
url: '/dashboard',
params: {
ticker: {},
tags: {}
},
views: {
'' : {
templateUrl: 'dashboard.html',
}
}
}
$stateProvider
.state(login)
.state(dashboard);
})
container.component('dashboardModule', {
templateUrl: 'dashboard.html',
controller: function($scope, $state) {
console.log('');
console.log('Dashboard component', $state.params);
}
})