在angularjs路由重定向 [英] redirect by routing in angularjs

查看:737
本文介绍了在angularjs路由重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求:
列表应显示与编辑所有项目,并删除链接。当用户点击编辑,编辑表单应显示文本框和保存按钮。现在,当用户编辑的数据,并点击保存按钮的数据应保存和列表页面应与修改后的数据再次出现。
一切工作正常,但怎样才能再次通过angularjs路由重定向到列表页面?
下面是一些code的:

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:

路由控制器:

    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'});
}]);

编辑表单:

    <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?

推荐答案

您需要注入的 $位置 editCtrl 控制器服务。

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');
}

此YouTube视频还可能会帮助你。

这篇关于在angularjs路由重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆