Angularjs Monitor Location Hash and Call Function on Match

I’m quiet newbie with AngularJS so I’ve been amazed with how complex but useful all it’s features can happen to be. The last one was on how to use the route mapper to solve one requirement in my Single Page App: I need to map a location.hash to a function, but AngularJS that doesn’t really work as Backbone. AngularJS maps it to a view and a controller. So, for the cases where you’d like to trigger an animation, scroll to a position in the page or expand a given element, it’s not useful!

I came up with this, but take extra notice and take it with scepticism. It’s just a prototype but I guess that the idea is pretty clear and I hope it helps solving your problems.

<!DOCTYPE html>
<html ng-app="MyApp">
<script src=""></script>
<script src=""></script>
<meta charset=utf-8 /> 
var MyApp = angular.module('MyApp', []);


MyApp.factory('routeMapToController', function($location,$q){

    var deferred = $q.defer();
    var path = $location.path();
    var matches = null;

        matches = path.match("\/issue\/(.+)\/(.+)");
        deferred.resolve("My resolved foo!");

    return deferred.promise;

MyApp.controller('MyCtrl', function($scope, routeMapToController){
  $ = "Hello World!";
  <div ng-controller="MyCtrl">

Thanks for looking!

comments powered by Disqus