1

I'm new to angularJS. I'm using angularjs to implement a website. The website has two pages. The first page list all items and if user click on it, it will redirect to detail information page.

I have my routes configure as below:

var app = angular.module('myApp', ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
    .when("/list", {
        templateUrl : "list.html",
        controller  : "listCtrl"
    })
    .when("/detail/:id"){
        templateUrl : "detail.html"
    }
});

In the list.html. I use ng-repeat to display a list of items and a listener on to listen mouse click.

<ul class="list-group">
    <li class="list-group-item" ng-repeat="i in items">{{i.name}}
      <a href="#" ng-click="goToEdit($index)">
        <span class="glyphicon glyphicon-pencil"></span>
      </a>
    </li>
</ul>

I want to redirect view to detail information in the ng-click function. I tried $location.path() but it not working.

$scope.goToEdit = function(index){
      console.log($location.path());
      $location.path("/detail/" + index);
      console.log($location.path());
}

This way won't work. Even though the second log on console show location.path() has been changed to " /detail/id".

If I add $scope.$apply() after $location.path(); Then I will get a action already in progress error.

Do you have any solution???

Thanks

2
  • let me see your controller, what are you injecting? Commented Oct 5, 2016 at 22:11
  • Can you reproduce the problem in a Plunker? Commented Oct 5, 2016 at 22:11

1 Answer 1

1

It might have something to do with use href="#" which is not good when using hash based routing

You could just set the href yourself and don't really need the controller function or ng-click

<a ng-href="#/detail/{{$index}}" >

Note that you should really give each item a unique identifier as $index could change due to using filters in ng-repeat or removing data from array

Also you don't have a controller identified for your second route

Sign up to request clarification or add additional context in comments.

5 Comments

Becomes a pain if you're using HTML5 mode, then you don't need the # prefix. Another reason why I like ui.router better
@Phil totally agree ui-sref makes it much simpler and html5Mode agnostic
Using $location.path should be HTML5 mode agnostic too. I can't see a reason for OP's code not to work
@Phil agree unless the hash is wigging out ngRoute ...easy enough to preventDefault too I guess
Ah yeah, didn't think of that. It probably is just redirecting to # from the href.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.