1

How do you pass a dynamic data in Angular 7.2.4? I've been researching this, and I don't see simple answer. On SO and Github they're saying to create a shared services, use data object on Routes. It seems that Angular doesn't have a functionality like this in React:

<Route
  path='/dashboard'
  render={(props) => <Dashboard {...props} isAuthed={true} />}
/>

Here's my current code:

<table>
  <tr>
    <th>id</th>
    <th>name</th>
    <th>username</th>
    <th>email</th>
    <th>delete</th>
  </tr>
  <tr *ngFor="let user of users">
    <td>
      {{ user.id }}
    </td>
    <td>
      {{ user.name }}
    </td>
    <td>
      {{ user.username }}
    </td>
    <td>
      {{ user.email }}
    </td>
    <td>
      <!-- <input type="checkbox" /> -->
      <button (click)="handleDeleteUser(user)">DEL</button>
    </td>
    <td>
      <button [routerLink]="['/edit', user]"> <-- problem here
        EDIT
      </button>
    </td>
  </tr>
</table>

On /edit url, user obj will populate. I don't want to pass and Id also and make a query from the backend because data is already there I just need to pass it. Is there a way?

4
  • Are you trying to pass an existing "user" object to a different component? Commented Feb 14, 2019 at 23:59
  • @hlfrmn yes sir. the user object has more data too. Commented Feb 15, 2019 at 0:04
  • holy crap. I figured it out. lols. I'am work and wasted 3hrs googling. smh. heres the answer. I used the 2nd answer link I hope they will make it simpler. Commented Feb 15, 2019 at 0:21
  • the above answer doesn't help if you just want send data. this solution exposes the parameters into the URL. this exposes my user information. not good. theres simple to pass ID or a param to the URL. Commented Feb 15, 2019 at 0:40

1 Answer 1

1

You can indeed use a service if you want to share an object with a different page.

The idea is pretty simple - you pass the user to some sort of service and then navigate to the page. The page, when it loads, will fetch the user from the service.

interface User {
    id: number;
    name: string;
    surname: string;
    email: string;
}

@Injectable({providedIn: 'root'})
export class ActiveUserProvider {
    private _user: User;

    public get user(): User {
        return this._user;
    }

    public set user(value: User) {
        this._user = user;
    }
}

We can inject this service in your component with the table and the edit button:

@Component()
export class TableComponent {
    // inject the service
    constructor(
        private activeUser: ActiveUserProvider,
        private router: Router)
    {}

    goToEditPage(user: User) {
        this.activeUser.user = user;
        this.router.navigate(['edit']);
    }
}

Just call the goToEditPage method from your button clicks:

<button (click)="goToEditPage(user)">EDIT</button>

And then your edit page would look like so

@Component()
export class EditUserComponent implements OnInit {
    public user: User;

    constructor(private activeUser: ActiveUserProvider) {}

    public ngOnInit() {
        this.user = this.activeUser.user;
    }
}

Some advantages to this approach:

  1. A separate service with strict typings to help control what is being shared between components
  2. Since it's a separate and injectable service, we can now easily test the two components separately: we can test that the table component sets the active user on "edit" click; we can also test that the edit component gets the active user on initialization.
Sign up to request clarification or add additional context in comments.

6 Comments

thank for this answer. let me try. my first solution is working but it exposes everything in the URL. crazy. I thought I solved it. but let me try your approach.
dude you are the code. but this is crazy how Angular implemented this. a lot of crazy ****. thank you my guy!
@artoo you're welcome :) when I have to use react I always miss my injection and services, so I suppose it works both ways
its cool to have services too. i like how they implement this. question my guy. the getter and setter for the shared service your updating the value like this. ` this.activeUser.user = user;` its running but is that correct? should it be this.activeUser.user(user) ?
@artoo setter and getter allow you to use a property this way - no mistake there. If it's confusing you could just define methods setUser(user: User) and getUser(): User instead
|

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.