7

EDIT: Added $routeProvider and $routeParams, but $routeParams.productId is always undefined. It was my initial try, but I thought it was the wrong way. Anyway it does not work for the moment.

I start to learn AngularJS and I have a very simple question : Depending on ID contained in URL, I would like to display different BD record.

...
<div ng-app=MyApp>
  <div ng-controller="MyCtrl">
    {{ record }}
  </div>
</div>
...

My Javascript file :

var MyApp = angular.module("MyApp",[]);

MyApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/get/:productId', {
       controller: 'MyCtrl'
    });
}])

MyApp.controller('MyCtrl',['$scope','$routeParams','$http',
  function($scope,$routeParams,$http) {
     $http.get("/get/"+$routeParams.productId).success(function(data) {
        $scope.record = data;
     });
}])

I tried to use $routeProvider and $routeParams without success.

Thank you in advance, Bill

1
  • It's totally possible that "get" in your path has a name collision, since it's also an HTTP verb and an $http method. Commented Jan 10, 2013 at 21:33

1 Answer 1

10

you need 2 things , the $routeParams injected in your controller and create a valid route with the get method

    MyApp.controller('MyCtrl',['$scope','$http',"$routeParams",
        function($scope,$http,$routeParams) {
             $http.get("/get/"+$routeParams.productId).success(function(data) {
                 $scope.record = data;
        });
    };
Sign up to request clarification or add additional context in comments.

3 Comments

What I am missing is how implement the glue linking ID used on URL, and $routeParams.productId. I have tried something like $routeProvider.when('/get/:productId', { controller: 'MyCtrl' }) without success.
Updated with $routeProvider
You need to install ngRoute for this to work: docs.angularjs.org/api/ngRoute

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.