angularjs - Angular ui-router doesn't change location -
i have abstract state header , content (really app has layouts, simplified code example). header has filter , want change location url dynamically click on filter category. working stopped, , don't know why...
i use similar pieces of code:
$stateprovider .state('root', { url: '', abstract: true, views: { '@': { templateurl: 'templates/layout.html', controller: 'layoutctrl' }, 'header@root': { templateurl: 'templates/header.html', controller: 'headerctrl' } } }) .state('root.index', { url: 'index?{filter}' }) }); (function() { 'use strict'; test.app.controller('layoutctrl', ['$scope', '$state' function($scope, $state) { $scope.params = $state.params; $scope.$on('params.changed', function($event) { $state.go($state.$current, $scope.params); // tried use // $state.go($state.current.name, $scope.params); }); }]); })(); (function() { 'use strict'; test.app.controller('headerctrl', ['$scope', '$state' function($scope, $state) { // event handler tasks , rewrite $scope.params $scope.params = { filter: [1, 2, 7, 15] }; // tried sent new data in event params $scope.$emit('params.changed'); }]); })();
but didn't understand, why ui state router doesn't change location.
updated:
it work on plnkr because there no mistake - in togglefilter function did $state.params = newparams; , ui-router didn't change becase parameters same.
i think example ui-router using:
angular.module('myapp', [ 'ui.router' ]) .config(function($stateprovider, $urlrouterprovider) { $urlrouterprovider.otherwise('/index?filter=1'); $stateprovider .state('root', { url: '', abstract: true }) .state('root.base', { url: '', abstract: true, views: { '@': { templateurl: 'layout.html', controller: 'rootcontroller' }, 'header@root.base': { templateurl: 'header.html', controller: 'headerctrl' } } }) .state('root.base.1', { url: '/index?{filter}' }) }) .controller('rootcontroller', function($scope, $state){ // when went page filter = 1 // object filter = 1 $scope.params = $state.params; $scope.$on('filter.changed', function($event, data) { $scope.params = data; console.log($scope.params); $state.go($state.$current, $scope.params); }); }) .controller('headerctrl', function($scope){ $scope.filters = [ { key: '1', value: 1, selected: false }, { key: '2', value: 2, selected: false }, { key: '3', value: 3, selected: false }, { key: '4', value: 4, selected: false }, { key: '5', value: 5, selected: false } ]; $scope.togglefilter = function(filter) { filter.selected = !filter.selected; var selectedfilters = [], filterlength = $scope.filters.length; (var = 0; < filterlength; i++) { $scope.filters[i].selected && selectedfilters.push($scope.filters[i].value); } $scope.$emit('filter.changed', { id: 1, filter: selectedfilters }); }; }) .run(function($rootscope, $location) { $rootscope.$on('$statechangesuccess', function(event, tostate, toparams, fromstate, fromparams) { console.log(fromstate); console.log('$statechangesuccess'); console.log($location.absurl()); console.log(tostate); }); })
Comments
Post a Comment