Friday, 6 September 2013

Angularjs converting a method shared between controllers to factory

Angularjs converting a method shared between controllers to factory

So I have a method that I'm using to basically redirect to a location in
my application.
changeLocation = (url, forceReload) ->
$scope = $scope or angular.element(document).scope()
if forceReload or $scope.$$phase
$window.location = url
else
$location.path(url)
$scope.$apply()
However, I have a couple SPA's engrossed in this application, and I want
to make this method available to all the different angular modules I have.
I got to this:
angular.module('location', []).factory('changeLocation', ['$scope',
'$location', ($scope, $location) ->
get: (url, forceReload) ->
$scope = $scope or angular.element(document).scope()
if forceReload or $scope.$$phase
$window.location = url
else
$location.path(url)
$scope.$apply()
])
Then I would add 'location' as a dependent on all my angular modules, and
inject changeLocation as a dependency in my controllers, however I
obviously get an error.

No comments:

Post a Comment