【发布时间】:2017-02-20 20:33:56
【问题描述】:
https://plnkr.co/edit/zfmuZXp88cSbdOsibv3y?p=preview
我有一个主要的 dashboard 父状态和两个子状态 tags 和 tickers。
当我点击tickers 中的按钮时,只有tags 状态应该刷新。目前它们都在刷新。
^ onInit tickersController console.log 应该只运行一次。然而,每次点击代码时都应该运行 tagsController。
var routerApp = angular.module('routerApp', ['ui.router']);
routerApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/dash');
var dash = {
name: 'dash',
url: '/dash?ticker',
params: {
ticker: 'AAA'
},
views: {
'': { templateUrl: 'dashboard.html' },
'tagsList@dash': {
url: '/tags',
templateUrl: 'tags-list.html',
controller: 'tagsController'
},
'tickersList@dash': {
url: '/tickers',
templateUrl: 'tickers-list.html',
controller: 'tickersController'
},
'alertsList@dash': {
url: '/alerts',
templateUrl: 'alerts-list.html',
controller: 'alertsController'
}
}
};
$stateProvider
.state(dash);
});
routerApp.controller('tagsController', function($scope, $state) {
$scope.ticker = $state.params.ticker;
function getList(ticker) {
switch(ticker) {
case 'AAA' : return ['aaa tag 1', 'aaa tag 2', 'aaa tag 3'];
case 'BBB' : return ['bbb tag 1', 'bbb tag 2', 'bbb tag 3'];
case 'CCC' : return ['ccc tag 1', 'ccc tag 2', 'ccc tag 3'];
}
}
$scope.tags = getList($state.params.ticker);
this.$onInit = function() {
console.log('onInit tagsController');
};
});
routerApp.controller('tickersController', function($scope, $state) {
$scope.changeScotchState = function(theTicker) {
$state.go('dash', { ticker: theTicker });
};
$scope.tickers = [
'AAA', 'BBB', 'CCC'
];
this.$onInit = function() {
console.log('onInit tickersController', $state.params.ticker);
};
});
routerApp.controller('alertsController', function($scope, $state) {
this.$onInit = function() {
console.log('onInit alertsController', $state.params.ticker);
};
});
【问题讨论】:
-
可能是状态变化?那么会不会影响门票查看?
-
是的,您将如何避免更改代码视图?或者其他任何孩子。基本上,我只希望仪表板的子项在他们需要的特定状态变量发生更改时被刷新。实际上,我将继续添加第三个控制器。
-
更新网址:'/tags',不是父视图?
-
ticker参数在dash上声明。每当ticker参数更改时,dash状态和所有子项都会重新加载。如果可以,请将ticker参数移至子状态。
标签: javascript angularjs angular-ui-router