-3

I want to AngularJS Change Path Without Reloading, my user case is to change URL that contain the some id, so user can share/send this url to friends. I looked AngularJS UI Router - change url without reloading state

in core.js:

'use strict';
angular.module('App', ['ngRoute'])
  .service('$locationEx', ['$location', '$route', '$rootScope',
    function($location, $route, $rootScope) {
      $location.skipReload = function() {
        var lastRoute = $route.current;
        var un = $rootScope.$on('$locationChangeSuccess', function() {
          $route.current = lastRoute;
          un();
        });
        return $location;
      };
      return $location;
    }
  ]);

In controller:

angular.module('App')
  .controller('DetailController', ['$scope', '$locationEx',
      function($scope, $locationEx) {
        $scope.changeURL = function() {
          console.log("IN changeURL");
          $locationEx.skipReload().path("sdfasdfasdfsadf").replace();
        };

If invoke changeURL, it will occur error:TypeError: $locationEx.skipReload is not a function

Can somebody help me? Thanks!

Community
  • 1
  • 1
UNSTABLE
  • 393
  • 1
  • 4
  • 13
  • What is the use case? Maybe all you need is have the data model state stored in a service and simply extend scope with it – charlietfl May 03 '15 at 13:56
  • hi, i want to call $locationEx.skipReload().path("sdfasdfasdfsadf").replace(); to change url without reload the page, i found the solution in [link](http://stackoverflow.com/questions/23585065/angularjs-ui-router-change-url-without-reloading-state), but it occur error. – UNSTABLE May 03 '15 at 13:58
  • that doesn't explain use case .. this well could be an [X-Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – charlietfl May 03 '15 at 14:34
  • @charlietfl use case is "to change url without reload the page" in angularJs, Thanks. – UNSTABLE May 03 '15 at 14:54
  • 1
    that doesn't explain why ... and is not a use case, it is a behavior description – charlietfl May 03 '15 at 14:58
  • If all you want to do is bookmark url I don't see why you need any of this. Sounds like a routing configuration or controller logic issue and as suspected this is definitely an X-Y problem. Show how you are using routeParams – charlietfl May 03 '15 at 15:35
  • @charlietfl , I just want change url completely to any url i want, why you care whether this is a XY problem? i have not used routeParams yet. – UNSTABLE May 03 '15 at 15:52
  • 1
    approach makes no sense ... good luck with it. Everything you need to be able to bookmark and share url is already built in if you use routing properly – charlietfl May 03 '15 at 16:03
  • @charlietfl please tell me how to use routing without load the page? – UNSTABLE May 03 '15 at 16:17
  • what does your `$routeProvider` config look like? – charlietfl May 03 '15 at 16:18

1 Answers1

0

I believe for a service you have to use 'this' when assigning properties within the service. So instead your service would look like:

'use strict';
angular.module('App', ['ngRoute'])
  .service('$locationEx', ['$route', '$rootScope',
    function($route, $rootScope) {
      this.skipReload = function() {
        var lastRoute = $route.current;
        var un = $rootScope.$on('$locationChangeSuccess', function() {
          $route.current = lastRoute;
          un();
        });
      };
    }
  ]);

Doesn't look like you need the $location service but I might be misunderstanding based on how you've phrased the question.

ecompton3
  • 76
  • 5