0

The problem I'm having here is not being able to find the right question to ask. I'd like to use a single partial and populate it with different data based on a url. The url would look something like this

localhost:8080/#/users/USER_ID

Where users directs to a user profile partial, and corresponding controller, and USER_ID would be sent in to an HTTP request to retrieve user data that would then populate the user profile partial.

Any direction in solving this is greatly appreciated.

2
  • Check ngRoute: docs.angularjs.org/api/ngRoute Commented Sep 2, 2015 at 6:05
  • Thank you @dfsq, this lead me to a very straight forward solution which I posted below! Commented Sep 2, 2015 at 20:14

3 Answers 3

1

If you are using ui-router which I highly recommend:

$stateProvider
      .state('users', {
      url:'/users/:userId',
      templateUrl: 'user.html',
      controller:'UserCtrl'
  })

You can then access the userId in your controller:

App.controller('UserCtrl', ['$scope', '$stateParams', '$state', 'User', function($scope, $stateParams, $state, User) {
'use strict';
/* controller code */

    var req = User.$find($stateParams.userId);

}]);

I am also using angular-rest-mod to make HTTP calls to an api when I do User.$find(id)

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

1 Comment

Thank you! This is exactly what I was looking for.
1

I found a solution that was a lot more straight forward than I had anticipated

app.js

    $routeProvider.
    when('/user/:id', {
        templateUrl: 'user.html',
        controller: 'userController'
    });

Then in the implementation of userController, $routeParams can be used to retrieve the value of id from the url.

Comments

0

Okay so I would probably go like this. First I would recommend Ui-Router instead of ngRoute this allows you to create your states for example

$stateProvider

  // setup an abstract state for the tabs directive
    .state('A', {
        params: [A,B,C,D,E],
        url: "/A",
        templateUrl: "templates/YourView.html",
        controller: "YourController"
      })

    .state('B', {
        params: [F,G,H,I,J],
        url: "/B",
        templateUrl: "templates/YourView.html",
        controller: "YourController"
      })

Basically this says when your Url is "/A" the "YourController" is used and the YourView.html is used so If I understood correct you have 1 view where you want to show different data depending on the Url.By Injecting 'ui.router'into your module and $state into your Controller you can access $state.current.params

Example Controller

.controller('ExampleController', ['$rootScope', '$scope', '$state', function ($rootScope, $scope, $state){
//Here you get your params
//This will return you either [A,B,C,D,E] or [F,G,H,I,J] depending if Url is /A or /B

      $scope.showList = $state.current.params;

//Another Possibility with this is to listen for a StateChange

      $scope.$on('$stateChangeSuccess', function (ev, to, toParams, from, fromParams) { 

//Here you can access all Params from the state you just left to the state you are going

});


}]);

Now you can just show this in the YourView.html like this

<div ng-repeat="item in showList">
<p>{{item}}</p>
</div>

So If your at /A the list shows A,B,C,D,E and if you are on /B it shows F,G,H,I,J

I hope this was helpful

1 Comment

Thank you for your answer! I really appreciate you taking the time to write this up and I learned something new from your answer. Sorry that my question wasn't worded as clearly as it could have been. What I was looking to achieve was having N possible URLs stemming from .../users/ where N is the number of registered users and the user ID is used to get user data via HTTP to populate the partial.

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.