Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect by routing in angularjs

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?

like image 348
z22 Avatar asked Sep 21 '12 10:09

z22


People also ask

What is routing in AngularJS?

Routing in AngularJS is used when the user wants to navigate to different pages in an application but still wants it to be a single page application. AngularJS routes enable the user to create different URLs for different content in an application.

What is use of $routeProvider in AngularJS?

$routeProvider is used to configure the routes. We use the ngRoute config() to configure the $routeProvider. The config() takes a function which takes the $routeProvider as parameter and the routing configuration goes inside the function.

What is routing in AngularJS and how does it work?

Routing in AngularJS is a method that allows you to create Single Page Applications. It enables you to create different URLs for different content in your web applications. AngularJS routing also helps to show multiple contents depending on which route is chosen. It is specified in the URL after the # sign.


1 Answers

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.

like image 130
Martin Avatar answered Oct 13 '22 08:10

Martin