0

I have the following AngularJS routing setup.

app.js

.config(function($stateProvider, $urlRouterProvider){
  //some code
  .state('app.edit', {
    url: '/edit/:recipeId',
    views: {
      'menuContent': {
        templateUrl: 'templates/recipes/edit.html',
        controller: 'editRecipeCtrl'
      }
    }
  //some code
});
$urlRouterProvider.otherwise('/app/home'); 

view

<a ng-click="editDemo()" href='#'>Edit</a>

The problem is when I click the link, it does not go to the edit page. (following are the observations)

  • No error in the console
  • I can see the url is changing in the address bar, but it quickly fall-back to the home page
  • If I type http://localhost/#/app/edit/1 in the address bar , it works
  • it calls the editDemo() method.

controller.js

$scope.editDemo = function(){
    // options I tried
     
    // $location.path( '/edit/1' );
    // $location.path( '#/app/edit/1');  
    // $location.path( 'edit/1');  
    // $location.url( '/edit/1' );
    // $location.url( '#/app/edit/1' ); 
    // $location.url( 'edit/1');
    // location.href = '/#/app/edit/1';
}
2
  • $location.path( '/edit/1' ); should be correct, try passing $event to ng-click callback and call $event.preventDefault() Commented Dec 11, 2014 at 13:39
  • @Rasalom, thanks for the quick answer, I updated the link to div <div ng-click="editDemo()">Edit</div> and code to $location.path( '/edit/1' );, but still doesnt work Commented Dec 11, 2014 at 13:45

2 Answers 2

2

I prefer (to prevent pitfalls) to use the $state service

$scope.editDemo = function(id) {
    $state.go('app.edit', {recipeId: id});
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have at least 2 ways here:

1) remove href from <a> tag: <a ng-click="editDemo()">Edit</a> and in editDemo:

$scope.editDemo = function(){
   $location.path( '/edit/1' );
}

2) work with click event in editDemo:

<a ng-click="editDemo($event)" href='#'>Edit</a>

$scope.editDemo = function($event){
     $event.preventDefault();
     $location.path( '/edit/1' );
}

1 Comment

thanks @Rasalom, I changed the code to Ben's way and its working now. and I believe its more like angular. But thanks again :)

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.