How to change URL without refreshing the Whole Page? - angularjs

I'm developing a site in angular and suppose I have navigated to iphone detail page and its current url is "localhost:8080/iphone/details" .
Now I have to implement a new functionality of applying coupons.In this suppose user clicks on some hyperlink from external site,
"localhost:8080/iphone/details?promotionalCode=12345"
User will land on that page(page will load after that modal will pop up) and a modal pop's up . And here details of promo code i will fetch from query parameter.
But my requirement is if i click cross (dismiss modal) ,and modal closes and state of url should be changed to localhost:8080/iphone/details .
What i have done till now .
I have used $location.search().promocode to fetch promocode if this is valid than modal will pop up and on dismiss modal i'm calling function in which i'm resetting state using $window.location.href , but this is making the whole page getting refreshed. Any suggestion on this ?

You will need to use $location.search().
To get the query parameter ?promotionalCode=12345, you will need to do $location.search('promotionalCode',12345).
Please see: AngularJS Documentation for $location

You can use $stateProvider for this just give the state
Example: .state('app.page', {
url: 'iphone/details',
title: 'Coupon Applied', })

Related

In angular js- how to redirect to home page , when user changed url

In angular js , how to redirct the url .
When user changed tab1 to tab2 in url, it must be redirect to tab1.
There are many ways to handle it.. Few of them:
Manage in your Routeconfig.
Manage it in your controller, by changing $location.path()
Manage via $locationChangeStart event listener. Like below :
scope.$on('$locationChangeStart', function(event, next, current) {
// manage navigation here.
});
See this attached Fiddle example, try with uncommenting code as well.

Ionic Back button - View does not show effect on localStorage change

I have a simple requirement where out of multiple pages, I have one Settings page in Ionic app, where I am allowing the user to toggle one data(say, language) that is maintained in LocalStorage in the app(via a factory).
This 'language' is used in all views(controllers).
I have Back button on the view but when user changes 'language' through Settings page(I update LocalStorage) and want to go back through IonicHistory back button to the preview view, change does not show up after going back.
"Previous view" uses LocalStorage.Language to fetch data
Back button uses following code:
$scope.goBack = function() {
window.history.back();
}
Can anybody help here or any workaround for this is possible.
Ionic caches views in the current navigation history. In order to update the view everytime you come to a view, you should use $ionicView lifecycle callbacks like .enter, .beforeEnter etc. The code written as part of these callbacks are executed everytime even if the view was cached:
$scope.$on('$ionicView.enter', function() {
// Get your settings data here to reflect it on page everytime
})
For details refer: http://ionicframework.com/docs/api/directive/ionView/

Ionic Back Button Doesn't Hide after exhausting $window.history.back()

I have an ionic app that has a search template that has a form where you can query posts by keyword. I also have a service that returns the post(s) as a list on another view. All that is working well.
My search controller upon submitting the search form uses:
$state.go('app.search.results', {'searchId': hash});
so that I can have a unique url for that search. I needed this unique url to implement 'back' functionality so that if a user clicks on one of the posts in the list, after viewing the post if they decide to click back, they would get to see the results of the search still (by default they would be returned to the search form without any results anymore).
To allow for a back to search results I implemented a custom back button function and put it on the ionic back button element like this:
<ion-nav-back-button ng-click="goBack()">
and then setup a the custom function:
$scope.goBack = function() {
$window.history.back();
}
All of this works well, I can go back to search results and see them, essentially very much like normal browser back functionality.
Problem for me is that when I have gone all the way 'back' via the back button, my initial state contains the 'Back' button and clicking it does not go anywhere and the 'Back' button still shows. Ionic does pretty good about hiding the back button when it shouldn't be there but in this case not so. Any ideas for how to check when history is exhausted and hiding the back button conditionally would be appreciated.
EDIT:
Here is a jsFiddle ; Note: open fiddle in a new, separate tab to see back button issue. FYI Search is in the menu.
One of the few qualms I have with Ionic is their "smart" navigation. I have run into a lot of problems with this myself. My solution was to create a back button of my own, this back button stores previous states and their params so you can go back and not lose search results for example with your scenario.
This component gives you both a back button and breadcrumbs to use (or you can just use back button)
It is open source and feel free to use it!
jscBreadcrumbs
Strange Milk - Breadcrumbs Post
Here is your jsFiddle with the jscBreadcrumbs implemented and working:
jsFiddle
jscbreadcrumbs
You use $window.history.back(), I think you should use $ionicHistory.goBack(); instead. It can control the history and view and state in the ionic way.

UI Router fails to update URL in address bar after successful state change

I'm building a small Angular app and I'm currently using ui-router.
I've hit a problem which looks awfully like a bug in ui-router but I can't be sure as I'm not that familiar with this library. When the user clicks on a specific link, although the correct view state gets loaded as expected, the URL in the address bar doesn't get updated.
I'm using ui-router's ui-sref directive to automatically generate the URL for the state. For example in the checklist list view I use the following code:
<a ui-sref="checklist-phase({ aircraftId: checklist.aircraft, checklistId: checklist.id, phaseSlug: checklist.phases[0].slug })" ng-bind="checklist.name"></a>
I've cut down my app and made it into a Plunker so the problem is hopefully reproducible by others. The issue can also be observed in this video: https://www.youtube.com/watch?v=EW9CFe6LfCw
Reproduction steps:
Go to http://run.plnkr.co/plunks/ZqMIYNU6abEAndAM5nx1/#/aircraft/1/checklists
Click the first link. Notice that the view updates to show the correct state, but the URL remains at /#/aircraft/1/checklists.
What is strange is that navigating back to this state by other means updates the URL perfectly. For example (assuming steps 1 and 2 above have been followed):
Scroll to the bottom and click the Next Phase link. Note that the state changes and the URL updates.
Scroll down on this new view and click Previous Phase. Note that the previous state reloads and this time the URL it updated correctly.
Am I using ui-router incorrectly or doing something else incorrectly to cause this behaviour?
Check here updated version
On your state 'check-lists' you provide ui-sref to 'checklist-phase'
<a ui-sref="checklist-phase({ aircraftId: ...
And the 'checklist-phase' is defined as a child state of 'checklist-detail'
.state('checklist-phase', {
parent: 'checklist-detail',
And the state 'checklist-detail' has controller which calls $state.go
.state('checklist-detail', {
controller: 'ChecklistDetailCtrl',
...
.controller('ChecklistDetailCtrl', function ($scope....
{
$state.go('checklist-phase', {
phaseSlug: checklistData.phases[0].slug
}, {
location: 'replace'
});
Do NOT do that... just remove the $state.go - because you are already navigating to the checklist-phase (see the first lines above) ... check it here

Backbone.js: How to utilize router.navigate to manipulate browser history?

I am writing something like a registration process containing several steps, and I want to make it a single-page like system so after some studying Backbone.js is my choice.
Every time the user completes the current step they will click on a NEXT button I create and I use the router.navigate method to update the url, as well as loading the content of the next page and doing some fancy transition with javascript.
Result is, URL is updated which the page is not refreshed, giving a smooth user experience. However, when the user clicks on the back button of the browser, the URL gets updated to that of a previous step, but the content stays the same. My question is through what way I can capture such an event and currently load the content of the previous step and present that to the user? Or even better, can I rely on browser cache to load that previously loaded page?
EDIT: in particular, I'm trying something like mentioned in this article.
You should not use route.navigate but let the router decide which form to display based on the current route.
exemple :
a link in your current form of the registration process :
<a href="#form/2" ...
in the router definition :
routes:{
"form/:formNumber" : "gotoForm"
},
gotoForm:function(formNumber){
// the code to display the correct form for the current url based on formNumber
}
and then use Backbone.history.start() to bootstrap routing

Resources