'How to assign one property as equal to other in angular?
I have a class, that I later use in angular component.
export class MyClass {
prop: string;
constructor(val){
this.prop = val;
}
public getLength(str: string): number {
return str.length;
}
}
The component:
@Component(...)
export class MyComponent implements OnInit{
public prop1: MyClass;
public prop2: MyClass;
constructor(private myService: MyService){}
ngOnInit(): void {
this.myService.getValue().subscribe(val => {
this.prop1 = val;
this.prop2 = val
});
}
public onResetEvent(): void {
this.prop1 = this.prop2;
}
....
}
When component works value of this.prop1 changes and from time to time I need to reset value of this.value1 to initial value (this.prop2 exists only for this one).
I try to use onResetEvent() function but it works only at first time (I reset this.prop1), but doesn't happen next times. Prop2 changes if prop1 changes.
How to solve my task?
Solution 1:[1]
Use cloneDeep() method of lodash-es for angular.
import * as lodash from 'lodash-es';
.
.
.
public onResetEvent(): void {
this.prop1 = lodash.cloneDeep(this.prop2);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Justwell Solets |
