Preventing page refresh when the back button is clicked in AngularJS

HTML Example:

<button type="button" class="btn btn-success btn-sm" ng-click="goBack($event);"> 
   Go Back
</button>

Model Example:

$scope.goBack = function ($event) {
   window.history.back();
   $event.preventDefault();
   $event.stopPropagation();
}

Answer №1

When transitioning in ui-router, the previous state is not tracked, however, the event $stateChangeSuccess is broadcast on the $rootScope when the state changes.

You can retrieve the prior state from that event (from being the state you're leaving).

$rootScope.$on('$stateChangeSuccess', function (event, toState,
   toParams, from, fromParams) {
       $rootScope.previousState = from.name;
       $rootScope.currentState = to.name;
}); 

Answer №2

When working with AngularJS applications, it is important to remember that they are designed as single page applications, meaning that the entire page should not refresh when navigating between different sections. One way to achieve this is by ensuring that the URLs contain a "#" symbol, which allows you to override the window.location.onhashchange event.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using JavaScript, include a child class into a parent class

I am facing an issue with my class hierarchy, which includes classes like Parent, Child1 (which extends Parent), and Child2. Parent contains an array of child items, but importing Child1 and Child2 into Parent leads to circular dependencies and errors whe ...

Calculation for determining the fill of a DIV's remaining height

Here is what I have: <div id="modal-container"> <div id="modal-body"></div> <div id="modal-footer"></div> </div> I am currently working on a JavaScript function to adjust the height of #modal-body to fill in the re ...

I have implemented the Angular "Date" filter, but I'm unsure about the meaning of this numerical value

The Angular tutorial showcases the date filter with the following example: {{ 1304375948024 | date }} --> May 2, 2011` How would you notate the expression 1304375948024? ...

Using React to pass a state value as a parameter in a method

Looking for a way to increase the reusability of my class. Dealing with a large form and don't want to create a unique method for each input. How can I pass the state value as a parameter to the method? I attempted the following approach: state = ...

Garbage collection is failing to release objects stored in the global array

Question regarding memory management in a JavaScript program. In my code, I have a global array called g_objArr, which is used to store customer profile details. This array contains objects with various customer information fields. The issue arises when ...

When pressed, the button changes color to a vibrant shade of blue, and not just a simple outline

I'm experiencing an issue with a button that turns blue when the user holds onto it for a while on mobile. You can see an example in the image below: https://i.sstatic.net/VmnZ8.png Adding ::selected or outline did not resolve the problem as the ent ...

Button Click Not Allowing Webpage Scroll

I am currently in the process of developing a website that aims to incorporate a significant amount of motion and interactivity. The concept I have devised involves a scenario where clicking on a button from the "main menu" will trigger a horizontal and ve ...

Tips on toggling between slides with Bootstrap Carousel

I am in the process of designing a portfolio website and have encountered a challenge. I want to divide it into two sections - a brief introduction and a carousel for showcasing projects. While I have managed to set up both sections, I'm facing an iss ...

Using Three.js: Adjust the UV coordinates of my texture within a specialized ShaderMaterial

Currently, I am working with a plane geometry and developing a CustomShader material for it. This material will require some textures as uniforms and I want these textures to completely cover my plane (similar to the background-size:cover CSS property). P ...

Adjusting the size of Bootstrap alerts while ensuring the close icon remains correctly positioned

Below is the HTML code snippet I am working with: <div class="row mt-2"> <div class="col-lg-5"> <div id="alertmessages"></div> </div> <div class="col-lg-7"> <div class="btn-group-sm"> ...

Uncovering mobile performance issues with AngularJS

Is there a way to identify performance problems with AngularJS on mobile devices using specific tools? While obvious issues like a large ng-repeat may be easy to spot, I'm interested in uncovering more subtle issues as well. Any suggestions for detec ...

Highlighting the current menu item by comparing the URL and ID

Looking to make my navigation menu items active based on URL and ID, rather than href. None of the suggested solutions (like this one, this one, or this one) have worked for me. This is how my Navigation is designed: <nav id="PageNavigation"& ...

Testing Angular 2 components utilizing ngModel

I am currently utilizing the most recent version of angular-cli along with angular 2.1. My objective is simple: I want to write a test for a component that retrieves data from a service and displays it. Everything functions as expected when the data is ju ...

What are the uses of AngularJS conditional statements?

Currently, I am following an AngularJS tutorial and encountering an issue. In the controller, I have an array with various points being displayed using ng-repeat {{feature.name}} {{feature.description}} My confusion lies in the fact that there is a third ...

Ensure one div scrolls independently while the other remains fixed using Bootstrap

I am currently in the process of constructing a web page with Bootstrap 4. The layout consists of two columns, each contained within individual divs. My requirement is for the content on the right side to be scrollable while the content on the left remains ...

Vue: downloading a file straight to your browser with the download attribute

I'm having an issue with axios where I am trying to download both a PDF and an image file. The image file downloads successfully and opens without any problems, but when I attempt to open the PDF file, it doesn't work as expected. downloadItem({ ...

issues with jasmine angularjs tests exhibiting erratic outcomes

During the execution of my unit tests, I often encounter a scenario where some random test(s) fail for a specific controller without any apparent reason. The error messages typically look like this: Expected spy exec to have been called with [ Object({}) ...

The issue with ng-bind-html causing the disappearance of paragraph spaces

Is there a way to preserve paragraph breaks when using ng-bind-html to display text on the screen? I am having an issue where all the text appears as one big chunk if there are paragraph breaks in the data pulled from the database. Not sure what is causing ...

Function that transposes two strings from top to bottom instead of left to right within an array

My current challenge involves transposing two strings vertically instead of horizontally using javascript. I'm contemplating whether to employ the map() and slice() methods for this task. The input will be an array containing two strings, and my goal ...

How to locate an element in Protractor using the name tag attribute

At the moment, I am utilizing protractor along with Selenium WebDriver. I am encountering an issue as described below: Upon clicking a button using protractor on a HTML page, a window pops up. This window contains a text box labeled "Description": <i ...