26

i have the following requirement: a list should be displayed for all items with edit and delete link. when user clicks on edit, edit form should appear with textboxes and a save button. now when user edits the data and clicks on the save button the data should be saved and the listing page should appear again with the modified data. everything works fine but the how do i redirect to the listing page again through routing in angularjs? below is some of the code:

ROUTING CONTROLLER:

    angular.module('productapp', []).
    config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/productapp', {templateUrl: 'partials/productList.html', controller: productsCtrl}).
        when('/productapp/:productId', {templateUrl: 'partials/edit.html', controller: editCtrl}).
        otherwise({redirectTo: '/productapp'});
}]);

edit form:

    <div>
    <form method="POST">
    <label>Add New Product:</label>
        <input type="text" name="keywords" ng-model="product.name" placeholder="enter name..." value="{{product.name}}">
        <input type="text" name="desc" ng-model="product.description" placeholder="enter description..." value="{{product.description}}">
        <button type="submit" ng-click="save(product.product_id,$event)" >Save</button>
    </form>
</div>

how do i redirect to the same listing page?

z22
  • 10,013
  • 17
  • 70
  • 126

1 Answers1

61

You need to inject the $location service in your editCtrl controller.

Then, in your save function add the following to do the redirect (note that the path matches your route).

$scope.save = function (...) {
    // ...
    $location.path('/productapp');
}

This Youtube video might also help you.

Martin
  • 8,876
  • 2
  • 35
  • 36
  • i tried this but then the page gets reloaded. i dont want the page to get reloaded. what else is the option? – z22 Sep 21 '12 at 12:41
  • See https://groups.google.com/d/topic/angular/eegk_lB6kVs/discussion, where the recommendation is to store the data in a service, and have a wrapper around it such that when the controller is re-initialized (when you go back to the page a second time), the wrapper decides whether to return the cached data or make an Ajax request. – Mark Rajcok Sep 24 '12 at 20:58