1

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'
        });
    });
2
  • Can you show us what you have tried till now? Commented Jul 11, 2016 at 6:14
  • check this question stackoverflow.com/questions/37130590/… please show some code but sure this will help you Commented Jul 11, 2016 at 6:27

1 Answer 1

2

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'
        });
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Erez for the help! This is what I was looking for... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.