Angular ui-router and back button with nested states - javascript

I'm having bad time with nested state, resolve and browser back button.
Below my states configuration, it consists of one abstract state and 2 children state, one to view a profile,
one to reserve it. The usual flow is opening the profile page, then click on some link and open the request page; this works fine. However if I click the browser back button on profile-request page, ProfileViewController is executed but profileData is not resolved.
Is there a way either to reload the previous view as it was (without having to rendering-it again) or force the promise resolution before executing the controller?
$stateProvider
.state('public', {
abstract: true,
templateUrl: 'app/public.html'
})
.state('public.profile-view', {
url: '/profile/:slug',
templateUrl: 'app/profile/profile.view.html',
controller: 'ProfileViewController',
controllerAs : 'vm',
resolve: {
profileData: function($stateParams, Profiles) {
return Profiles.query({ slug: $stateParams.slug });
}
}
})
.state('public.profile-request', {
url: '/profile/request/:slug',
templateUrl: 'app/profile/profile.request.html',
controllerAs: 'vm',
controller: 'ProfileRequestController',
resolve: {
profileData: function($stateParams, Profiles) {
return Profiles.query({ slug: $stateParams.slug });
}
}
})

Have you tried caching the profileData in Profiles and then in ProfileViewController try to get cached data if it is existing.
Another option is storing profileData in the $rootScope.

Related

Resolve promise in abstract parent before $stateChangeStart fires

I'm attempting to resolve some server-side data in an abstract parent state before the Home child state is loaded. I want to make sure I have the user's full name for examination in the state's data.rule function. However, using console.log statements, I can see the userFullName prints as an empty string to the console before the "done" statement in the factory.privilegeData method.
EDIT
The data in the parent resolve is security data from the server that needs to be available globally before any controllers are initialized. Only one child state is listed here (for the sake of readability), but all states, outside of login are children of the abstract parent. Once the parent's resolves are complete the data is stored in the $rootScope and used to determine access rights via the data.rule function in each state.
config.js
UPDATE: per answer below I've updated the parent/child states as such, howeve I'm still running into the same issue:
.state('app', {
url:'',
abstract: true,
template: '<div ui-view class="slide-animation"></div>',
resolve:{
privileges: ['$q', 'privilegesService', function($q, privilegesService){
console.log('from parent resolve');
return privilegesService.getPrivileges()
.then(privilegesService.privilegesData)
.catch(privilegesService.getPrivilegesError);
}]
}
})
.state('app.home', {
url: '/home',
templateUrl: 'app/components/home/home.html',
controller: 'HomeController',
controllerAs: 'vm',
parent: 'app',
resolvePolicy: {when:'LAZY', async: 'WAIT'},
authenticate: true,
data:{
rule: function($rootScope){
console.log("from home state data rule");
return true;
}
}
})
privilegesService.js
factory.getPrivileges = function () {
console.log('from server call');
var queryString = "&cmd=privilege&action=user";
return $http.get(config.serviceBaseUri + queryString);
};
factory.privilegesData = function(priv){
console.log('from privilege data');
if(priv && priv.data) {
$rootScope.userFullName = priv.data.firstName + ' ' + priv.data.lastName;
}
console.log('done');
};
Based on the console statments above I'm getting the following output. I need the from home state data rule to occur last...
...since I'm using the the results of the data.rule function for authorization for each state. Below is the $stateChangeStart from my .run method
app.route.js
$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
if(toState.authenticate){
if(authService.authKeyExists()){
if(toState.data.rule($rootScope)){
navigationService.addNavObject("activity", {
summary : "Page navigation",
page : $location.absUrl().replace("#/", "")
});
} else {
$state.go('app.home');
}
} else {
event.preventDefault();
$state.go('login');
}
}
});
The child states need the parent state as a dependency in order for it to wait to load, even if its not used directly. Source
you have to try something like the following instead :
.state('app.home', {
url: '/home',
templateUrl: 'app/components/home/home.html',
controller: 'HomeController',
controllerAs: 'vm',
parent: 'app',
authenticate: true,
data:{
rule: function($rootScope){
console.log($rootScope.userFullName);
return true;
}
}
})
You can use the property resolvepolicy and set it to LAZY as well, a state's LAZY resolves will wait for the parent state's resolves to finish before beginning to load.

AngularJS - $stateProvider when user put wrong URL

I'm new angularjs student.
I'm using state provider in my project, i don't want to change this. Because the code is done.
Here is my code:
function config($stateProvider, $urlRouterProvider) {
$urlRouterProvider
.when('/SecondMain', '/SecondMain/OtherPageOne')
.when('/Main', '/Main/PageOne')
.otherwise("/notfound")
$stateProvider
.state('Main', {
abstract: true,
url: "/Main",
templateUrl: "/templates/Common/Main.html"
})
.state('SecondMain', {
abstract: true,
url: "/SecondMain",
templateUrl: "/templates/Common/SecondMain.html"
})
.state('notfound', {
url: "/NotFound",
templateUrl: "/templates/Common/NotFound.html"
})
.state('Main.PageOne', {
url: "/Main/PageOne",
templateUrl: "/templates/Main/PageOne.html"
})
.state('Main.PageTwo', {
url: "/Main/PageTwo",
templateUrl: "/templates/Main/PageTwo.html"
})
.state('SecondMain.OtherPageOne', {
url: "/SecondMain/PageOne",
templateUrl: "/templates/SecondMain/OtherPageOne.html"
})
.state('SecondMain.OtherPageTwo', {
url: "/SecondMain/PageTwo",
templateUrl: "/templates/SecondMain/OtherPageTwo.html"
})
angular
.module('inspinia')
.config(config)
.run(function ($rootScope, $state) {
$rootScope.$state = $state;
});
}
I want a logic like this: If the user put:
/Main/PageThree
This page does not exist, but the user start URL with
/Main
so that he need to go to -> /Main/PageOne
if the user put:
/Ma/PageOne
/Ma does not exist, the user starts URL totally wrong, so that he goes to -> /Notfound Basically if the user put /Main/WRONG_LINK, he go to /Main/PageOne . And if he does not start with /Main, he go to NotFound.
Can anyone help me please?
Thanks a lot!!!
You are missing this configuration
$urlRouterProvider.otherwise('/NotFound');
Just add this line if no matching route is found then it will redirect you to the /NotFound url
This answer is inspired by this answer.
First of all, you will have to make the Main state non-abstract, so that it can be visited. Then, you can write config related to where you want to redirect (for example, I've used redirectTo with the state):
$stateProvider
.state('Main', {
redirectTo: "Main.PageOne",
url: "/Main",
templateUrl: "/templates/Common/Main.html"
})
// ... Rest of code
So, whenever the URL is changed to /Main, this state will get activated. The second config will be to create a listener for $stateChangeStart event as follows:
angular
.module('inspinia')
.config(config)
.run(function ($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function(evt, to, params) {
if (to.redirectTo) {
evt.preventDefault();
$state.go(to.redirectTo, params, { location: 'replace' })
}
});
});
Now, if a URL like /Ma/* is hit, it automatically be redirected to /NotFound. And if a URL like /Main is hit, it will redirect to /Main/PageOne.
You can follow up on further discussion on this link for any kind of troubleshooting.
Clearly read the statements below
Why are you using this line?
when('/Main', '/Main/PageOne')
For your redirection problem, have a look at the below state
.state('Main', {
abstract: true,
url: "/Main",
templateUrl: "/templates/Common/Main.html"
})
abstract: true ==> This denotes that this particular state is an abstract which can never be activated without its child.
SOURCE: ui-router js code. Refer the below snippet
Since you have this main state as abstract, you are redirected to the otherwise.
Hope this

$ionicHistory.backView() returning wrong view

When I am using $ionicHistory.backView() it is returning the view that was before the view previous to the current view, so it is going one step back than it should be. But when I refresh the page on the view previous to the current one, where I am testing $ionicHistory.backView() it is returning null. For some reason I am not getting anything for that specific view.
This is my code:
.controller('RegisterController', function($scope, $state, UserService, $auth, MessageService, $ionicHistory) {
$scope.user = UserService.get();
$scope.validate = {};
$scope.registerPromise = {};
console.log($ionicHistory.backView());
And these are the routes:
.state('main.login', {
url: '/login',
views: {
'content': {
templateUrl: 'templates/login.html',
controller: 'AuthController',
}
},
authenticate: false
})
.state('main.login2', {
url: '/login2',
views: {
'content': {
templateUrl: 'templates/login2.html',
controller: 'AuthController',
}
},
authenticate: false
})
.state('register.contact', {
url: '/contact',
views: {
form: {
templateUrl: 'templates/user/register-contact.html',
}
}
})
So, the flow is when the user is on the main.login page, he can either go to the register.contact page where he leaves the contact details, or go to the main.login2 page where he can login or reset the password by going again to register.contact page where he can again send his contact details for reseting the password. That is why I need to know for my form where is the user coming from, since I am using the same form for two things, leaving the contact details for registering and reseting the password. But I am not getting the correct data from $ionicHistory.backView()

ui-router accessing the child directly

I've got a nested view ui-router application, whenever I access the child with controller directly, the parent data is not filled (but is called, and I can see logs)
what is wrong?
.state('home.app', {
url: '/apps/:package_name',
abstract: true,
templateUrl: 'app/components/apps/details/app_header.html',
controller: 'AppController',
controllerAs: 'appCtrl'
})
.state('home.app.details', {
url: '/', templateUrl: 'app/components/apps/details/app.html'
})
.state('home.app.images', {
url: '/images', templateUrl: 'app/components/apps/details/images.html',
controller: 'ImagesController',
controllerAs: 'imagesCtrl',
resolve: {
package_name: function($stateParams: IStateParamsService) {
return $stateParams.package_name;
}
}
})
when I access the default child (details child) the parent loads and everything is ok (view is filled with data) but no luck with images, the AppController is being called but the view is not filled
if I'm not allowed to have such structure, what is my alternative? I have some shared data, and some specific data that should load on child states (tho I can load them all in parent controller but I guess that is not the way to go, since parent controller should not load submodels)

How to reload a child view without reloading whole state in AngularJS

I have a state that has multiple views declared in it as follows:
$stateProvider
.state('home.details.item', {
url: '^/details',
views: {
'chartsView': {
templateUrl: 'charts.html',
controller: 'chartsCtrl'
},
'gridView': {
templateUrl: 'grid.html',
controller: 'gridCtrl'
},
'detailsView': {
templateUrl: 'details.html',
controller: 'detailsCtrl'
}
}
});
I need to reload one of the views without reloading the whole state, without using $state.go($state.current,null , {reload: true}) , and if possible, from the chartCtrl reload detailsCtrl. Is that possible?
I'd say, that the UI-Router solution should be built arround *states*, not views.
(I created working example here). Other words, if there are
some views which should not be reloaded and
some other views, which should be reloaded
... it calls for state nesting. Let's move that view into child state:
.state('home.details.item', {
url: '^/details',
views: {
'chartsView': {
templateUrl: 'tpl.charts.html',
controller: 'chartsCtrl'
},
'gridView': {
templateUrl: 'tpl.grid.html',
controller: 'gridCtrl'
},
// 'detailsView': {
// templateUrl: 'details.html',
// controller: 'detailsCtrl'
// }
}
})
.state('home.details.item.more', {
views: {
'detailsView#home.details': {
templateUrl: 'tpl.details.html',
controller: 'detailsCtrl'
}
}
})
We also need a state, which will do the reload. We could use other way, e.g. with some changing parameter in state more, but that would mean to change the param value on each call. With this specil state, we can easily reload our state 'more':
.state('reload', {
parent: "home.details.item",
views: {
'detailsView#home.details': {
// this controller will just redirect to 'more' and make it fresh...
controller: ['$state', function($state) { $state.go('^.more')}],
}
}
})
And with these simple controllers we can do all that required stuff:
.controller('chartsCtrl', function ($scope, $state) {
var childName = ".more";
$state.go(childName); // default is a sub state 'more'
})
.controller('detailsCtrl', function ($scope) {
$scope.when = Date.now();
})
Having this: we can call this to reload just details:
<a ui-sref="reload">force reload detail view</a>
Now, when navigating to reload, we will be redirected to state "more" and our view will be rerendered.
SUMMARY:
In general, UI-Router represents state machine. I would strongly suggest:
Do not worry to think in states. Views are just their representation in the DOM.
If there are some features related, they most likely represent state. If others do not relate (should be changed often or rarely) they belong to other state. It could be parent, child or sibling...
Check it here

Categories

Resources