How Do I Integrate Angular.js With RxJS?
Last updated
Was this helpful?
Last updated
Was this helpful?
is a popular MV* framework for JavaScript which covers things such as data binding, controllers as well as things such as dependency injection. The Reactive Extensions for JavaScript plays well with this framework, and in fact has a dedicated library for interop called . However, if you don't wish to use that, here are some simple ways you can integrate the two together.
The is an object that refers to the application model. It is an execution context for expressions. Scopes are arranged in hierarchical structure which mimic the DOM structure of the application. Scopes can watch expressions and propagate events.
Scopes provide the ability to observe change mutations on the scope through the method. This is a perfect opportunity to integrate the power of the Reactive Extensions for JavaScript with Angular. Let's look at a typical usage of $watch
.
Using the Reactive Extensions for JavaScript, we're able to easily bind to this by wrapping the $watch
as an observable. To do this, we'll create an observable sequence using Rx.Observable.create
which gives us an observer to yield to. In this case, we'll capture both the old and new values through our listener function. The $watch
function returns a function, which when called, ceases the watch expression.
Now that we have this, we're able to now take the above example and now add some RxJS goodness to it.
Using the Reactive Extensions for JavaScript, we can also integrate using the Rx.Observable.fromPromise
bridge available in RxJS version 2.2+. We simply
These are just only the beginnings of what you can do with the Reactive Extensions for JavaScript and AngularJS.
AngularJS ships a promise/deferred implementation based upon called the service. Promises are quite useful in scenarios with one and done asynchronous operations such as querying a service through the service.