Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a property from a simple object using ngrx redux and angular2

I'm using angular2 2.0.0-beta.2 with ngrx store https://github.com/ngrx/store and implemented a simple app using a modified version of the "counter" example on the ngrx/store github page.

In my app, I have a "user" object with two buttons for login and logout. I also have the corresponding reducer functions login and logout where I either return the state with an object that includes a name property or with an empty object...

export const user: Reducer<any> = (state: any = {}, action: Action) => {

    switch (action.type) {
        case LOGIN:
            return {name: 'jdoe'};

        case LOGOUT:
            return {};

        default:
            return state;
    }
};

This doesn't update:

<h2>User Name = {{ user.name | async }}</h2>

...It remains blank when I click the login button, but when piped through the 'json' pipe, I see that indeed the object has changed...

This does update

{{ user | async | json }}

...which outputs { "name": "jdoe" } but obviously that does me no good. Using simply {{ user.name | async }} doesn't work and remains blank.

the template:

<div>
    <h2>User Name = {{ user.name | async }}</h2>
    <button (click)="login()">login</button><button (click)="logout()">logout</button>
</div>
<hr>
<div>
      {{ user | async | json }}
</div>

Here's the plunker: http://plnkr.co/edit/cPubI9upph344qrOqtj8?p=preview

like image 568
inki Avatar asked Oct 14 '25 11:10

inki


1 Answers

Syntax looks like (user | async).name - see http://plnkr.co/edit/OVHh9lHvTU4sKrCbrLqq?p=preview

like image 136
robwormald Avatar answered Oct 17 '25 03:10

robwormald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!