Search code examples
angularjsangularjs-directiveangularjs-scopeangular-ui-routerangularjs-ng-repeat

TypeError: Cannot read property 'hash' of undefined


I am facing an error while i am trying to call the following angularjs method with id and $event as parameter

    $scope.gotoLink = function(id, $event) {
        $event.preventDefault();

        var currentLocation = $location.hash();

        $timeout(function() {
            $location.hash(id);
            $anchorScroll();
            // reset location to initial value,
            // required to stop modal window auto-closing
            $location.hash(currentLocation);
        });
    };

Error is as follows

TypeError: Cannot read property 'hash' of undefined at Scope.$scope.gotoLink (http://upfdev/ui/accounts/xyzcontroller.js:68:41) at Parser.functionCall (http://upfdev/ui/bower_components/angular/angular.js:10847:21) at ngEventDirectives.(anonymous function).compile.element.on.callback (http://upfdev/ui/bower_components/angular/angular.js:19136:17) at Scope.$get.Scope.$eval (http://upfdev/ui/bower_components/angular/angular.js:12702:28) at Scope.$get.Scope.$apply (http://upfdev/ui/bower_components/angular/angular.js:12800:23) at HTMLAnchorElement. (http://upfdev/ui/bower_components/angular/angular.js:19141:23) at HTMLAnchorElement.jQuery.event.dispatch (http://upfdev/ui/bower_components/jquery/jquery.js:5095:9) at HTMLAnchorElement.jQuery.event.add.elemData.handle (http://upfdev/ui/bower_components/jquery/jquery.js:4766:28)

I am not sure why this error when I have already inject $location. Eagerly awaiting any help..Spent a good time on this finding the issue


Solution

  • You have to add this:

    .controller('DetailController', ['$scope', '$location', function($scope, $location)