具有动态参数传递的Angular JS路由控制 [英] Angular JS routing control with dynamic parameter passing

查看:100
本文介绍了具有动态参数传递的Angular JS路由控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AgularJ的新手,并且正在开发单页应用程序。我被困在需要向下一个模板发送动态ID(即/ pagename& id =)的位置,并且也需要使用ng路由进行控制。有没有办法使用此动态ID值来处理路由网址?

I'm new to AgularJs, and I'm working on a single page application. I was stuck on a position where i need to send an dynamic id to next template (i.e /pagename&id= ) and that needs to be control using ng routing as well. Is there a way to handle routing url with this dynamic id value?

这是重要的代码段

//Controller function call here passing the dynami id on 'data-value'
<button type="submit" data-value="<dynami id>" ng-click="submit();">

//controller function
var app = angular.module('myApp', []);
var id = <Dynamic id>;
app.controller('galleryController', function($scope, $http, $location) {
    $scope.submit = function() {
        $location.path('/gallary-single&id='+id);
    }
});

//Routing...
app.config(function($routeProvider) {
        $routeProvider
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })
        .when('/gallary', {
            templateUrl : 'pages/gallary.html',
            controller  : 'galleryController'
        })
        .when('/gallary-single', {
            templateUrl : 'pages/gallary-single.html',
            controller  : 'gallerySingleController'
        });
    });


推荐答案

您应该尝试

<button type="submit" ng-click="submit(_id);">

//var id = <Dynamic id>; --  no need for that
app.controller('galleryController', function($scope,  $location) {
  $scope._id = 'some-id';
    $scope.submit = function(id) {
        console.log('submit id:', id);
        $location.path('/gallary-single/' + id);
    }
});

//Routing...
app.config(function($routeProvider) {
        $routeProvider
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })
        .when('/gallary', {
            templateUrl : 'pages/gallary.html',
            controller  : 'galleryController'
        })
        .when('/gallary-single/:id', {
            templateUrl : 'pages/gallary-single.html',
            controller  : 'gallerySingleController'
        });
    });

这篇关于具有动态参数传递的Angular JS路由控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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