'The Async pipe not trigger even when getter of observable is called
I have a question. Maybe one of you can help me. I have a situation when async pipe in component not trigger even then subscribition get of this observable is triggering (component not reviving changed value in ngOnChanges).
Code is like this:
html:
<app-component [values]="values$ | async"></app-component>
ts:
values$: Observable<ISomeValue[]> = this.service.getValue$();
service:
private valuesBSubject: BehaviorSubject<ISomeValue[]> = new BehaviorSubject<ISomeValue[]>(this.defaultValues);
getValues$(): Observable<ISomeValue[]> {
return this.valuesBSubject.asObservable();
}
setValues(values: ISomeValue[]): void {
this.valuesBSubject.next(values);
}
Solution 1:[1]
You can't do that using member functions. However, you can do that using a generic function (or an algorithm). As your members are private, you'll need to make the algorithm a friend, though:
#include <iostream>
#include <vector>
template <typename T> void print(T const&);
template <typename T> void addNum(T&, int);
class Base
{
template <typename T> friend void print(T const&);
template <typename T> friend void addNum(T&, int);
private:
std::vector<int> nums;
};
class Derived : public Base
{
template <typename T> friend void print(T const&);
template <typename T> friend void addNum(T&, int);
private:
std::vector<int> nums;
};
template <typename T>
void print(T const& obj) {
for (auto x: obj.nums) {
std::cout << x << " ";
}
std::cout << "\n";
}
template <typename T>
void addNum(T& obj, int num) {
obj.nums.push_back(num);
}
int main()
{
Base b;
addNum(b, 1);
addNum(b, 2);
print(b);
Derived d;
addNum(d, 3);
addNum(d, 4);
addNum(static_cast<Base&>(d), 5);
print(static_cast<Base&>(d));
print(d);
}
I don't necessarily think that an approach like that is well-advised. However, it is certainly doable. A variation of a similar idea is to actually access the nums container through a virtual function, yielding the respective version of the corresponding derived class:
class Base
{
private:
std::vector<int> d_nums;
virtual std::vector<int> &nums() { return this->d_nums; }
public:
void addNum(int num) { nums().push_back(num); }
};
class Derived : public Base
{
private:
std::vector<int> nums;
std::vector<int> &nums() override { return this->d_nums; }
};
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 | Dietmar Kühl |
