Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 behavior subject with class object

I have below class

export class filter {
    public PageRecords: number;
    public SearchText: string;
    public SortColumn: string = null;
    public SortDirection: string = null;
    public StartingPos: number;
}

Below behavior subject

filterParam = new BehaviorSubject(new filter);

Updating subject

this.filterParam.next(<filter>{StartingPos  : 1,PageRecords : 10 })

When I get the value of subject

this.filterParam.value

It only have two props StartingPos and PageRecords which I updated. it lost other props.

How to resolve this?

like image 757
Md. Parvez Alam Avatar asked Oct 16 '22 07:10

Md. Parvez Alam


1 Answers

It is because you don't pass directly a Filter object class. You are casting some dynamic json to a filter object but only with this two properties.

You can do something like this:

const filterParam = new filter();
filterParam.StartingPos = 1;
filterParam.PageRecords = 10;

this.filterParam.next(filterParam);

EDIT: I didn't notice that you want just update two values from the BehaviorSubject value. As @ritaj suggested in a comment below this aswer you can do something like he suggested:

this.filterParam.next(Object.assign(this.filterParam.value, {StartingPos: 1, PageRecords: 10 }

Object.assign() copies the values (of all enumerable own properties) from one or more source objects to a target object. It has a signature of Object.assign(target, ...sources). The target object is the first parameter and is also used as the return value. Object.assign() is useful for merging objects or cloning them shallowly.

like image 124
XardasLord Avatar answered Oct 20 '22 05:10

XardasLord