dimanche 3 mai 2015

AngularJs, how to call function in service?

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!

Aucun commentaire:

Enregistrer un commentaire