AngularJS Change Path without reloading

Source: Internet
Author: User

To change path URL with AngularJS, $location. Path () are passed the new URL as a parameter, and the page is automatically re Loaded. This means so if you ever make a change to the URL of the Angular application, then the partial and controller would be re Loaded automatically. This was great most of the time, however, there was times when reloading the controller was unwanted. AngularJS doesn ' t had a native fix to stop reloading when the path was changed. So a quick addition to our App.js file would do the trick.

Solution

The solution is essentially to add a extra parameter to $location. Path (). So rather than just taking in the new URL, it'll also take in a Boolean were true would refresh the page and false would n Ot. The following block of code is a needs to be added to the App.js file.

App.run ([' $route ', ' $rootScope ', ' $location ', function ($route, $rootScope, $location) {    var original = $ Location.path;    $location. Path = function (path, reload) {        if (reload = = = False) {            var lastroute = $route. Current;            var un = $rootScope. $on (' $locationChangeSuccess ', function () {                $route. Current = Lastroute;                Un ();            });        }        Return original.apply ($location, [path]);}    ])

  

There you go, now here's what it'll look like in the controller where the path is being changed.  

$location. Path ('/sample/' + $scope. Checkinid, False);

  

Well, that's all there was to it, we're now, and the change path of the the URL without reloading the page. If you had any questions, comments or suggestions feel free to leave a comment!

AngularJS Change Path without reloading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.