通过动态参数传递

时间:2016-07-11 06:07:03

标签: angularjs routing

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

这是重要的代码片段

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

1 个答案:

答案 0 :(得分:2)

你应该试试

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