Angular JS routing control with dynamic parameter passing

不想你离开。 提交于 2019-12-13 01:28:35

问题


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?

This is the code snippets that matters

//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:


you should try that

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


来源:https://stackoverflow.com/questions/38300146/angular-js-routing-control-with-dynamic-parameter-passing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!