I\'ve been looking at these pages (1, 2, 3). I basically want to change my $state
, but I don\'t want the page to reload.
I am currently in the page
For this problem, you can just create a child state that has neither templateUrl
nor controller
, and advance between states
normally:
// UPDATED
$stateProvider
.state('schedules', {
url: "/schedules/:day/:month/:year",
templateUrl: 'schedules.html',
abstract: true, // make this abstract
controller: function($scope, $state, $stateParams) {
$scope.schedDate = moment($stateParams.year + '-' +
$stateParams.month + '-' +
$stateParams.day);
$scope.isEdit = false;
$scope.gotoEdit = function() {
$scope.isEdit = true;
$state.go('schedules.edit');
};
$scope.gotoView = function() {
$scope.isEdit = false;
$state.go('schedules.view');
};
},
resolve: {...}
})
.state('schedules.view', { // added view mode
url: "/view"
})
.state('schedules.edit', { // both children share controller above
url: "/edit"
});
An important concept here is that, in ui-router
, when the application is in a particular state—when a state is "active"—all of its ancestor states are implicitly active as well.
So, in this case,
schedules
(along with its templateUrl
, controller
and even resolve
) will still be retained.Adding my answer because I think it's different enough from the accepted answer and may be useful to others:
I had two states, begin
and view
, with a bunch of optional parameters being synced with the URL for view
, like so:
$stateProvider
.state('begin',
{
url: '/',
template: '<app-element></app-element>'
})
.state('view',
{
url: '/View?param1¶m2&...¶mN',
template: '<app-element></app-element>'
params: {
param1: {
value: null,
squash: true
},
...
}
});
The link function for <app-element>
would run any time I tried to sync the parameters using $state.go
. Using {notify: false, reload: false}
did not work for me. The link function still ran each time. I'm on 0.2 so dynamic
isn't an available param option, either. I followed @b0nyb0y's suggestion and turned it into a parent/child relationship, which worked:
$stateProvider
.state('app',
{
url: '/',
template: '<app-element></app-element>'
})
.state('app.view',
{
url: 'View?param1¶m2&...¶mN',
params: {
param1: {
value: null,
squash: true
},
...
}
});
REF: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options
$state.transitionTo('yourState', params, {notify: false});