0

How to set two dynamic : id in url angularjs? I do not know how to set idUsers to automatically get id from url. I have error

The following parameter values are not valid for state 'user': [usersId:undefined]))

I want to go on the route: /{usersId}/workers/{userId}

users/users.html

<ul>
    <li ng-repeat='user in $ctrl.users'>
        <div>
          <a ui-sref="user({usersId: users.id, userId: user.id })" href='#'> 
             {{user.name}}
          </a></div>
        <div>{{user.id}}</div>
    </li>
</ul>

user/user.js

.config(($stateProvider) => {
  "ngInject";
 $stateProvider
    .state('user', {
      url: '/{usersId}/workers/{userId}',
      component: 'user',     
      resolve: {
        user: (UserService, $stateParams, $q) => {
          let deferred = $q.defer();
          UserService.getUser($stateParams.userId)
            .then(success => {
              deferred.resolve(success.data);
          });
          return deferred.promise;
        },      
      }
   });
})
  • Avoid the [deferred anti-pattern](https://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg May 20 '19 at 12:45
  • @georgeawg error `The following parameter values are not valid for state 'user': [usersId:undefined]))` –  May 20 '19 at 13:11
  • Maybe `user` and `users` is switched. `User` is used in so many places it is hard to tell what is what. `User` is a state, a `ng-repeat` iterator, a property name, etc. Obviously `users.id` is `undefined`. – georgeawg May 20 '19 at 13:33
  • @georgeawg How can I refer to $ stateParams.usersId? Can I set this.usersId = $ stateParams.usersId in controller in users.js? Then bind usersId and I will refer to the user view? –  May 20 '19 at 13:59

1 Answers1

0

First of all, not use href whit ui-sref.

A directive ui-sref that binds a link ( tag) to a state. If the state has an associated URL, the directive will automatically generate & update the href attribute via the $state.href() method. Clicking the link will trigger a state transition with optional parameters. Also middle-clicking, right-clicking, and ctrl-clicking on the link will be handled natively by the browser.

<a ui-sref="home">Home</a> 

is converted to:

<a href="#/home" ui-sref="home">Home</a>

Second, it seems that users.id dosen't have value. Debug users and user variables.