【发布时间】:2017-03-16 16:11:09
【问题描述】:
https://plnkr.co/edit/n2RANdhtRYIGQURVIElY?p=preview
预期
login 后,当您点击 Count 时,Tags 列表中的红色数字应该会发生变化并与 Tickers 列表中的数字匹配,但 Feed 列表中不会发生任何事情.
结果
登录后,点击Tickers列表中的Count,红色的数字同时出现在Tags列表和Feed列表中。
完整的 plnkr 代码
var tickers = angular.module('tickers', ['ui.router'])
tickers.config(function($stateProvider) {
const tickers = {
name: 'tickers',
url: '/tickers',
parent: 'dashboard',
templateUrl: '<p></p>'
}
$stateProvider
.state(tickers);
})
tickers.component('tickersModule', {
templateUrl: 'tickers-list.html',
controller: function($scope, $state) {
console.log('Tickers init', $state.params)
$scope.counter = 0;
$scope.increase = function() {
$scope.counter++;
$state.go('tags', { counter: $scope.counter }).then(function() {
});
}
}
})
var tags = angular.module('tags', ['ui.router'])
tags.config(function($stateProvider) {
const tags = {
name: 'tags',
url: '/tags',
parent: 'dashboard',
params: {
counter: 0
},
template: '<p class="counter">{{ counter }}</p>',
bindToController: true,
controller: function($scope, $state) {
console.log('tags state object', $state)
$scope.counter = $state.params.counter;
}
}
$stateProvider
.state(tags);
})
tags.component('tagsModule', {
templateUrl: 'tags-module-template.html',
controller: function($scope, $state) {
}
})
var feed = angular.module('feed', ['ui.router'])
feed.config(function($stateProvider) {
const feed = {
name: 'feed',
url: '/feed',
parent: 'dashboard',
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', $state.params)
}
})
var routerApp = angular.module('routerApp', ['ui.router', 'tickers', 'tags', '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',
templateUrl: 'dashboard.html',
controller: function($state) {
console.log('Dashboard state init', $state)
}
}
$stateProvider
.state(login)
.state(dashboard);
})
.run(['$rootScope', '$location', '$state',
function($rootScope, $location, $state) {
$rootScope.$on("$stateChangeError", console.log.bind(console));
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams, options) {
// console.log(' ')
// console.log('toState', toState)
// console.log('toParams', toParams)
// console.log('fromState', fromState)
// console.log('fromParams', fromParams)
// console.log('options', options)
});
}]);
tags-module-template.html
<div class="jumbotron text-center">
<h2>Tags list</h2>
<div ui-view></div>
</div>
feed-module-template.html
<div class="jumbotron text-center">
<h2>Feed list</h2>
<div ui-view></div>
</div>
与 routerApp 模块中的仪表板状态关联的仪表板.html。包含所有 3 个组件:
<div class="jumbotron text-center">
<h1>The Dashboard</h1>
</div>
<div class="row">
<tickers-module></tickers-module>
<tags-module></tags-module>
<feed-module></feed-module>
</div>
标签模块、状态配置和组件代码:
var tags = angular.module('tags', ['ui.router'])
tags.config(function($stateProvider) {
const tags = {
name: 'tags',
url: '/tags',
parent: 'dashboard',
params: {
counter: 0
},
template: '<p class="counter">{{ counter }}</p>',
bindToController: true,
controller: function($scope, $state) {
console.log('tags state object', $state)
$scope.counter = $state.params.counter;
}
}
$stateProvider
.state(tags);
})
tags.component('tagsModule', {
templateUrl: 'tags-module-template.html',
controller: function($scope, $state) {
}
})
Feed 模块、状态配置和组件代码
var feed = angular.module('feed', ['ui.router'])
feed.config(function($stateProvider) {
const feed = {
name: 'feed',
url: '/feed',
parent: 'dashboard',
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', $state.params)
}
})
【问题讨论】:
-
您是否尝试过将
tags和feed添加到您的状态,就像dashboard和login(带有名称、templateUrl 和不同的控制器)?然后,做ui-view="tags"和ui-view="feed"或类似的东西? -
我在某个时候尝试了
ui-view="tags",但没有成功,但我希望将这些模块分开。最终目标是更改一个模块中的某些内容不一定会导致另一个模块刷新/重新初始化。 -
这很奇怪。通常
ui-view没有值是指index.html...
标签: javascript angularjs angular-ui-router