angularJS HTML模板中的相对兄弟URL

时间:2016-04-22 09:21:32

标签: angularjs routing

说我在http://localhost/#/edit/10/step2页面上,我希望有一个链接(锚标记),将我带到http://localhost/#/edit/10/step1

如果没有在链接中指定id(ng-href=""),可以在10中添加什么才能实现?

<a ng-href="step1">Previous</a>

编辑(路由):

.config(["$stateProvider", "$urlRouterProvider", "$httpProvider", ($stateProvider: ng.ui.IStateProvider, $urlRouterProvider: ng.ui.IUrlRouterProvider, $httpProvider: ng.IHttpProvider) => {
        $urlRouterProvider.otherwise("/home");

        $stateProvider
            .state("MyModule", {
                template: "<div ui-view></div>"
            })
            .state("home", {
                url: "/home",
                templateUrl: "App/home.html",
                controller: "homeController as ctrl"
            });

        $httpProvider.interceptors.push("busyHttpInterceptor");
    }]);

陈述(编辑):

            .state("edit", {
                url: "/edit/:id",
                templateUrl: "App/edit.html",
                controller: "editController",
                controllerAs: "ctrl",
                resolve: {                    
                }
            })

1 个答案:

答案 0 :(得分:0)

我认为你可以通过ng-click函数进行重定向:

//for the 'state', you can add a new one or edit the existing one depending on your requirement and restirctions
//Add a new one:
.state("edit.action", {
    url: "/edit/:id/:action",
    templateUrl: "App/edit.html",
    controller: "editController",
    controllerAs: "ctrl",
    resolve: {                    
    }
})

//in the html
<a ng-click=goToAction("step1")>Previous</a>

//in the controller 'editController', inject the service '$stateParams' and '$state'
$scope.goToAction = function(action) {
    //get the ':id' from the url
    var currentId = $stateParams['id'];
    $state.go('edit.action', {
        id: currentId,
        action: action
    });
}

如果需要,还可以将regex应用于url的参数部分:

url: "/edit/{id:[a-zA-Z0-9]*}/:action