Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I can't get the param of the URL which I pass when changing the state in Angular (ui router):

.state('contact.detail', {
    url: '/:contactId',
    templateUrl: 'detail.html',
    controller: 'DetailController'
})

In Express I define an API, but the problem is in getting the param from the URL which I passed from ui router (above).

server.js

var express = require('express');
var mysql = require('mysql');
var url = require('url');

var app = express();

app.use('/', express.static('../app'));
app.use('/bower_components', express.static('../bower_components/'));

var server = require('http').createServer(app);

var bodyParser = require('body-parser');
app.jsonParser = bodyParser.json();
app.urlencodedParser = bodyParser.urlencoded({ extended: true });

//mysql connection setup
var connection = mysql.createConnection({
    host : "localhost",
    port: "3306",
    user : "root",
    password : "",
    database : "db",
    multipleStatements: true
});

app.get('/:id', app.urlencodedParser, function(req,res){

    var id = req.params.id;

    console.log(id); // => :id instead of value

    connection.query('SELECT * FROM contacts WHERE contactId = ?', [id], function (error, results) {        
        if(error) {
            throw error;
        }
        else { 
            res.end(JSON.stringify(results));
        }
    });
});

server.listen(3000, function () {
    'use strict';
});

In log I get ":id" instead of the real value, for example "45".

I can access the API manually

enter image description here

Please take a look at the plunker for states details.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Since u are using ui-router (or ngRoute) , it is client side routing , if you want to call a route from your server you have to make a http call , with $http service (or $resource) , like:

 //this is a example not tested.
.controller('DetailController', function($scope, $stateParams,$http){
  console.log('Passed parameter contact id is:', $stateParams.contactId);

  $scope.selectedContactId = $stateParams.contactId;
  $http.get("localhost:3000/"+$stateParams.contactId)
         .success(function(data){
                //console.log(data)
         })
         .error(function(error,status){

         })
    });
share|improve this answer
    
Thanks cshion, I didn't mention that I have http call with $http service and it's look like this $http.get('/:id').success(function (data) { data.forEach( function( row, index ) { $scope.myData.push(data); }); $scope.gridOptions.data = data; }) When I hardcode id variable in server.js to "45" (for example), data are succesfully displayed on the frontend. Problem is in getting this value (45) in API (server.js) when I click (change state) on the frontend. – corry Nov 6 at 19:19
1  
are you passing StateParams.contactId to $http call , something like .. $http.get(''/""+$stateParams.contactId) . ? – cshion Nov 6 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.