I came across some code in Angular that rotates between words every few seconds:
words: string[] = ['foo', 'bar', 'baz'];
word = null;
rotateWords() {
const source = Rx.Observable.interval(1000).take(this.words.length);
const sub = source.finally(this.rotateWords).subscribe(i => this.word = this.words[i]));
}
Unfortunately, I get the error message "Rx is not found" in on my version of Rx (6.5.5) and Angular (10.0.9). The code seems to be written in an old-style of RxJs. How do I rewrite it in the new style?
If you are using angular with rxjs 4+
import { interval } from 'rxjs'
import { take, finalize} from 'rxjs/operators'
words: string[] = ['foo', 'bar', 'baz'];
word = null;
rotateWords() {
const source = interval(1000).pipe(take(this.words.length));
const sub = source.pipe(finalize(this.rotateWords.bind(this))).subscribe(i => {
this.word = this.words[i];
});
}
}
Thanks, I am getting some errors on the second line:
const sub...
arounderror TS1135: Argument expression expected. 19 const sub = source.pipe(finally(this.rotateWords)).subscribe(i => this.word = this.words[i]));
I have changed
finally
tofinalize
as this was renamedThanks, I tried this and the above and it doesn't seem to look. When it gets to the last word, I get the error
1 errors occurred during unsubscription: 1) TypeError: Cannot read property 'length' of undefined
Sorry meant to say, it doesn't seem to "loop"
Please check now, I binded
this