Angular UI Router reload data on state change
I am using Angular ui router (https://github.com/angular-ui/ui-router) and coming across an unexpected behavior.
I have a recordView
and recordEdit
states set up and using sref/$state.transitionTo to switch between them.
When in recordEdit
state, update is being done via ajax and upon success, I am programatically chaning the state into recordView
. Problem is that the recordView
state does not show the update data and will only show it if I refresh the page.
I tried using the reload option but with no success.
App.saveRecord($scope.formData).then(function (response) {
$state.transitionTo('recordView', $stateParams, {
reload: true
});
}
I also tried using $state.go(...)
but getting the same result.
I also tried using the cache = false
on the state property but with no success.
.state('recordView', {
url: '/folder/:hash/:recordId',
resolve: {},
cache: false,
templateUrl: function (urlattr) {
//return the url
}
})
I then tried explicitly changing the window.location
to the view url but it will still show the previous data. The only time it will actually work is if I call location.reload();
after changing the state but this is not good for the user experience.
Does anyone know why this is happening? all the posts I've seen about it mention setting the reload to true or the cache to false.
UPDATE Per the comments I understand that the problem is that I am using ng-init and server side rendering to inject the data from php to angular and when reloading the view, this data is not reloading. My questions then are:
recordEdit
state into the recordView
state after the user edited the data? Thanks.
Here is an idea. On your routes file define a parent abstract state and initialize that with the data
.state('parent.state'
{
abstract: true,
data:{
init://server rendered data here
}
}
)
.state('child.state.view',
{
// your route definition here
}
)
.state('child.state.edit',
{
// your route definition here
}
)
then in your controllers inject the $state service in yout view controller use
//assuming the data variable holds the needed data. change that to what ever
$scope.data = $state.current.data.init; //if using $scope
this.state = $state.current.data.init; //if controller As syntax
Remove the ng-init sentence as the data will be initialized on the controller. Then on the Update function in the edit view make sure you update the $state.current.data.init
with the new data. This way next time you go there the controller will pick the data from the $state object and get the updated one.
回答:
$templateCache.remove('http://urlthemplate');
链接地址: http://www.djcxy.com/p/77948.html