6

I use routeProvider in Angular JS:

.when('/profile/personal', {
       templateUrl: 'personal.html',
       controller: 'EditProfileController'
})

How I can pass param to controller EditProfileController and here call Ajax method that returns data. This data must be display in template of route in personal.html.

Example:

    .controller('EditProfileController', ['$scope', '$http', function ($scope, $http) {
         // If was click on `/profile/personal` from route, must get patam `personal` and call method GetAjax(param);
         $scope.GetAjax = function (param){
            //here return data put it in template HTML from route
         }  

    }]);

My links are located in page by path:

http://wd.tk/profile

When I click to link route I get URL:

http://wd.tk/profile#/profile/personal/1

Id do:

.controller('EditProfileController', ['$scope', '$http', function ($scope, $http, $routeParams) {
   console.log($routeParams);
}]);

I get error:

TypeError: Cannot read property 'idProfile' of undefined
3
  • where is this param coming from? Is it from the route Url? Commented May 2, 2015 at 15:02
  • If tell otherwise, when I click link from route, I must call AJAX in controller and responded data must be in template from route Commented May 2, 2015 at 15:10
  • You can specify a parameter in the route url with "/profile/personal/:param", and the param is available via the injectable $routeParams, so, in the controller you can do $routeParams.param Commented May 2, 2015 at 15:17

2 Answers 2

10

First, in your url configuration, you must put the parameter of url:

when('/profile/personal/:idProfile', {
       templateUrl: 'personal.html',
       controller: 'EditProfileController'
})

Now, in your EditProfileController, you should get the parameter and call to ajax request:

Inject the $routeParams object and get the parameter with

$routeParams.idProfile:

.controller('EditProfileController',  
function ($scope, $http, $routeParams) {

   var idProfile = $routeParams.idProfile;

   $http.get("service url").then(setData, setError);

   function setData(data) {
       $scope.data = data;
   }
   function setError() {
       alert("error on get data profile");
   }

}]);

In your html, you will show your data profile in the correct format.

But I recommend that all the ajax calls should groups in angular services. PD: Check It out angular ui router:

What is the difference between angular-route and angular-ui-router?

hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

One moment please, what link must be for /profile/personal/:idProfile is <a href="/profile/personal/1">Link</a>?
So, but how to check if was click on personal and do request Ajax then. Now your code in controller is run always
Also I get error: TypeError: Cannot read property 'idProfile' of undefined
0

You you need to change your $routeProvider, that should have /profile/:type instead of /profile/personal that means you are going to provide value for :type which can be accessible by injectin $routeParams service inside controller.

.when('/profile/:type', {
       templateUrl: 'personal.html',
       controller: 'EditProfileController'
})

Controller

.controller('EditProfileController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
     $scope.profileType = $routeParams.type;
     $scope.GetAjax = function (){
        //here you have profile type in $scope.profileType
        //you can pass it to ajax by simply accessing scope variable.
        $http.get('/getprofiledata?type='+ $scope.profileType)
        .success(function(data){
           $scope.profileData = data;
        })
        .error(function(error){
           console.log('error occured')
        })
     }  
     $scope.GetAjax(); //calling it on controller init
}]);

Update

YOu had missed one of dependency $routeParams in array annotation.

.controller('EditProfileController', ['$scope', '$http', '$routeParams',function ($scope, $http, $routeParams) {
   console.log($routeParams);
}]);

6 Comments

How be if I need set when like as: .when('/profile/contacts/', { templateUrl: 'personal.html', controller: 'EditProfileController' })
then you need to add one more when condition like $routeProvider.when('/profile/:type', { templateUrl: 'personal.html', controller: 'EditProfileController' }).when('/profile/contacts/', { templateUrl: 'personal.html', controller: 'EditProfileController' })
Can you see please again my question and edit own answer? Because now are a lot moments
It works thabnks, can I check if exists $routeParams.type = 1?
@Hamed could you do upvote & accept my answer..Thanks :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.