AngularJS:如何调用函数的服务? [英] AngularJS : how to call function in service?
问题描述
我要AngularJS更改路径没有重新加载,
我的用户的情况是更改包含一些ID的URL,这样用户可以共享/发送该URL给朋友。
我看了<一个href=\"http://stackoverflow.com/questions/23585065/angularjs-ui-router-change-url-without-reloading-state\">AngularJS UI路由器 - 变更网址无需重新加载状态
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
在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;
}
]);
在控制器:
angular.module('App')
.controller('DetailController', ['$scope', '$locationEx',
function($scope, $locationEx) {
$scope.changeURL = function() {
console.log("IN changeURL");
$locationEx.skipReload().path("sdfasdfasdfsadf").replace();
};
如果调用changeURL,就会发生错误:类型错误:$ locationEx.ski preLOAD不是一个函数
If invoke changeURL, it will occur error:TypeError: $locationEx.skipReload is not a function
有人可以帮我吗?谢谢!
Can somebody help me? Thanks!
推荐答案
我相信你必须使用一个服务'这个'分配服务中的属性时。因此,而不是你的服务看起来像:
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.
这篇关于AngularJS:如何调用函数的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!