clear text input on another controller - angularjs

is it possible to clear all fields in one controller by clicking on a button from the previous controller?
I'm trying to do so by setting cache: false in routes.js in the controller that needs to be cleared, however, I realized that it clears the controller's fields every time it's loaded.
I need to be able to clear the fields of the pages once a user clicks on login, however, I also need to be able to go back to the previous pages once I enter the "review info" page.
How should I go about this?

If you are using ionic version-1 framework, then use
$scope.logout = function(){
// first, clear the history
$ionicHistory.clearHistory();
// clear the cache before you navigate to a page
$ionicHistory.clearCache().then(function(){
$state.go("app.login");
});
}

Related

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/

Get the url that a user is coming from AngularJS

If an user is coming from an specific page i need to do get some values out of a cookie and change what the users sees.
Now, the issue is that i cannot find a way to view what page the user is coming from.
EDIT: This is intended to capture when the users clicks back in a page and save the state of the previous page.
Any ideas?
Thanks in advance
Solved. Every time i load a page i'm saving the url, so when i get to this page i just have to read it to tell. Thanks!
You can use browser history in our javascript or you can write your last page in cookies and get the last link then update it
Using cookies will indeed fix this for you. So when a user goes to a new page - set a cookie like:
app.controller('myController',['$scope', '$location', $cookies], function($scope, $location, $cookies){
if($cookies.get('page') == '/index'){
//do stuff if user came from index
}
$scope.pageChanged = function(value){
$cookies.put('page', value);
$location.path('/index');
}
}
just make sure you use the pageChanged function to set your page every time user changes pages.
Using the $routeProvider you can use the resolve function to detect when a new route has been loaded.
Another way would be to listen for the event $routeChangeSuccessor $routeChangeError and get the information needed from the service $location or $route.
If you want a sample just ask me, I'll try to post one as soon as I have free time.

How to change URL without refreshing the Whole Page?

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', })

Cannot correctly refresh page using AngularJS

I have a page, which allows the user to add/edit/delete database table.
The database staff, and add/edit/delete, all work fine.
However, in my AngularJS method, I add location.reload(), trying to refresh the page to show the updated table. Unfortunately, the database has been updated, but the page isn't.
If I press F5 to manually reload, the result is right. I don't use route in the page.
Just want a simple function, and it can automatically refresh after being updated.
Below is sample codes
$scope.add = function(){
//add something in database
//no problem of this part
location.reload() //doesn't work
};
Try adding the $ and the location service to the controller
MyController($scope, $location){
$scope.add = function(){
$location.reload();
}
}

close/leave page event with AngularJS

I am trying to implement a single page application that starts with a login form. For front-end I'm using AngularJS.
In the login's controller I check the authentication data, and if it is ok, I set a cookie (using $cookieStore service) with some user data and route to another view. Here, in the controller of the second view I check if the user from the cookie is empty and if yes, I redirect to the login view.
What I want to do is when the user close the browser, or if leaves the page the cookie to be removed.
I have tried to use:
$scope.$on("$locationChangeStart", function(){
$cookieStore.remove('user');
});
or
$scope.$on("$destroy", function(){
$cookieStore.remove('user');
});
but it does not work.
I want to avoid the scenario when a user log in (the cookie is set) and is redirected successfully to the second view, and after this close the browser/leaves the page (the cookie is still there). Another user write the url of the second view and because the cookie with the data of the first user is still there, he succeed to authenticate.
Can anyone help me with this?
plunker link
I am not sure. But maybe the browser closes before you even do anything in your logic.
Try this :
$scope.$on("$locationChangeStart", function(event){
event.preventDefault()
$cookieStore.remove('user');
});
try this code, it will works
$scope.$on('$locationChangeStart',function(event) {
if($scope.formsubmitted && $scope.myForm.$dirty){
var answer = confirm("Are you sure you want to leave this page?")
if (!answer) {
event.preventDefault();
}else{
$cookieStore.remove('user');
}
}
});

Resources