RxJS and using with Angular 1
up vote
4
down vote
favorite
I am working on an integration between Angular 1.5 app with RxJS. I was thinking of using RxJs directly but then I found the following although its not been updated for a few months.
https://github.com/Reactive-Extensions/rx.angular.js
Which is the recommended way ? Also when subscribing where should this be done ? In the controller? Or in services ?
Does anyone have any more information? I would really like to integration my Angular app with RxJs but I am finding it difficult to find concrete documentation or guides.
angularjs reactive-programming rxjs
add a comment |
up vote
4
down vote
favorite
I am working on an integration between Angular 1.5 app with RxJS. I was thinking of using RxJs directly but then I found the following although its not been updated for a few months.
https://github.com/Reactive-Extensions/rx.angular.js
Which is the recommended way ? Also when subscribing where should this be done ? In the controller? Or in services ?
Does anyone have any more information? I would really like to integration my Angular app with RxJs but I am finding it difficult to find concrete documentation or guides.
angularjs reactive-programming rxjs
An explanation of the use case would improve the question, it is too broad in its current state. If it is $scope that is observed, it is done in controller for sure. The official repo has no tutorials, but there are examples and exhaustive API documentation.
– estus
Jun 4 '16 at 15:45
Ok. More specifically then, would I be better using the angular extension for rxjs or using rxjs direct?
– Martin
Jun 4 '16 at 23:37
1
I guess, this depends on your use case, too. Check how much of this is applicable to you. Notice that it is still on its way to support RxJS 5.
– estus
Jun 5 '16 at 6:50
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I am working on an integration between Angular 1.5 app with RxJS. I was thinking of using RxJs directly but then I found the following although its not been updated for a few months.
https://github.com/Reactive-Extensions/rx.angular.js
Which is the recommended way ? Also when subscribing where should this be done ? In the controller? Or in services ?
Does anyone have any more information? I would really like to integration my Angular app with RxJs but I am finding it difficult to find concrete documentation or guides.
angularjs reactive-programming rxjs
I am working on an integration between Angular 1.5 app with RxJS. I was thinking of using RxJs directly but then I found the following although its not been updated for a few months.
https://github.com/Reactive-Extensions/rx.angular.js
Which is the recommended way ? Also when subscribing where should this be done ? In the controller? Or in services ?
Does anyone have any more information? I would really like to integration my Angular app with RxJs but I am finding it difficult to find concrete documentation or guides.
angularjs reactive-programming rxjs
angularjs reactive-programming rxjs
edited Nov 10 at 19:15
halfer
14.2k757105
14.2k757105
asked Jun 4 '16 at 11:44
Martin
9,11838165277
9,11838165277
An explanation of the use case would improve the question, it is too broad in its current state. If it is $scope that is observed, it is done in controller for sure. The official repo has no tutorials, but there are examples and exhaustive API documentation.
– estus
Jun 4 '16 at 15:45
Ok. More specifically then, would I be better using the angular extension for rxjs or using rxjs direct?
– Martin
Jun 4 '16 at 23:37
1
I guess, this depends on your use case, too. Check how much of this is applicable to you. Notice that it is still on its way to support RxJS 5.
– estus
Jun 5 '16 at 6:50
add a comment |
An explanation of the use case would improve the question, it is too broad in its current state. If it is $scope that is observed, it is done in controller for sure. The official repo has no tutorials, but there are examples and exhaustive API documentation.
– estus
Jun 4 '16 at 15:45
Ok. More specifically then, would I be better using the angular extension for rxjs or using rxjs direct?
– Martin
Jun 4 '16 at 23:37
1
I guess, this depends on your use case, too. Check how much of this is applicable to you. Notice that it is still on its way to support RxJS 5.
– estus
Jun 5 '16 at 6:50
An explanation of the use case would improve the question, it is too broad in its current state. If it is $scope that is observed, it is done in controller for sure. The official repo has no tutorials, but there are examples and exhaustive API documentation.
– estus
Jun 4 '16 at 15:45
An explanation of the use case would improve the question, it is too broad in its current state. If it is $scope that is observed, it is done in controller for sure. The official repo has no tutorials, but there are examples and exhaustive API documentation.
– estus
Jun 4 '16 at 15:45
Ok. More specifically then, would I be better using the angular extension for rxjs or using rxjs direct?
– Martin
Jun 4 '16 at 23:37
Ok. More specifically then, would I be better using the angular extension for rxjs or using rxjs direct?
– Martin
Jun 4 '16 at 23:37
1
1
I guess, this depends on your use case, too. Check how much of this is applicable to you. Notice that it is still on its way to support RxJS 5.
– estus
Jun 5 '16 at 6:50
I guess, this depends on your use case, too. Check how much of this is applicable to you. Notice that it is still on its way to support RxJS 5.
– estus
Jun 5 '16 at 6:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
I put together a small JSFiddle demonstrating a modified example from the RxJS-Angular Extensions Project. EDIT My first example was very similar to the one in the resources for click events. I've made the example a little more complex, and it examples using a Subject to publish and subscribe to data that is handled/modified by a factory that returns a promise.
Versions note: Rx-Angular, as of 09.28.2017, only supports RxJS version 4. RxJS is a dependency to RxAngular, so be careful or you'll run into errors you cannot resolve.
In short, though, my answer is to use the Rx-Angular extensions library. Partly because RxJS is a dependency for Angular-RX, so you'll have it anyway and can use both. But also because it allows for some simple Observable crafting that can be useful, like for events or "$watches".
https://jsfiddle.net/ejmLpyrn/3/
HTML
<div ng-app="myApp" ng-controller="appCtrl">
<!-- RxJS v4.1.0, RxAngular v1.5-->
<div class="jumbotron">
<div class="form-group">
<input type="text" ng-model="query" />
<button type="button" ng-click="clickFunction()">
Search
</button>
</div>
</div>
<hr>
<div>{{message}}</div>
</div>
JS
angular.module('myApp', ['rx'])
.factory('subjectProxyService', function () {
var subjectProxyService = {};
var subject = new Rx.Subject();
subjectProxyService.subject = function () {
return subject;
}
return subjectProxyService;
})
.factory('appFactory', function(rx) {
function addContextToMessage(query) {
var deferred = $.Deferred();
var timestamp = moment().format("YYYY-MM-DD");
var _msg = "[" + timestamp + "]" + query;
deferred.resolve(_msg);
return rx.Observable
.fromPromise(deferred.promise())
.map(function(contextMsg) {
return contextMsg;
});
}
return {
addContextToMessage: addContextToMessage
};
})
.controller('appCtrl', function($scope, rx, appFactory, subjectProxyService) {
$scope.query = "";
$scope.message = "";
subjectProxyService.subject().subscribe(
function (x) { console.log('Value published to observer: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted');
});
$scope.$createObservableFunction('clickFunction')
.map(function () {
return $scope.query;
})
.flatMapLatest(appFactory.addContextToMessage)
.map(function(results) {
return results;
})
.subscribe(subjectProxyService.subject());
// give the service-subject as the subscriber to the "click stream"
})
You can do pure RxJS inside of Angular for sure, but you can also write mostly jQuery inside your Angular app if you really wanted. So long as all the libraries are loaded you're just running JavaScript inside of Angular. I would consider it mostly best practice and also future-smart to use the Angular extensions, though, which you can also expect to support Angular and RxJS changes. Not to mention, they've taken the time to write a bunch of tested helper and constructor methods you don't have to write for yourself -- like mapping/linking a $watch on a $scope variable to an Observable.
Resources
Here's some more resources for Rx-Angular. There are for instance 3 helpful methods that you can use to create Observables inside of Angular. They're basically helpful constructor methods for RX Observables (so you don't have to write your own!):
$toObservable
$toObservable - $scope variables
$eventToObservable
$eventToObservable - $events
$createObservableFunction
$createObservableFunction - binding functions in scope to an Observable, like ngClick
Using Subjects in RxJS
Subjects AS-A Service
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I put together a small JSFiddle demonstrating a modified example from the RxJS-Angular Extensions Project. EDIT My first example was very similar to the one in the resources for click events. I've made the example a little more complex, and it examples using a Subject to publish and subscribe to data that is handled/modified by a factory that returns a promise.
Versions note: Rx-Angular, as of 09.28.2017, only supports RxJS version 4. RxJS is a dependency to RxAngular, so be careful or you'll run into errors you cannot resolve.
In short, though, my answer is to use the Rx-Angular extensions library. Partly because RxJS is a dependency for Angular-RX, so you'll have it anyway and can use both. But also because it allows for some simple Observable crafting that can be useful, like for events or "$watches".
https://jsfiddle.net/ejmLpyrn/3/
HTML
<div ng-app="myApp" ng-controller="appCtrl">
<!-- RxJS v4.1.0, RxAngular v1.5-->
<div class="jumbotron">
<div class="form-group">
<input type="text" ng-model="query" />
<button type="button" ng-click="clickFunction()">
Search
</button>
</div>
</div>
<hr>
<div>{{message}}</div>
</div>
JS
angular.module('myApp', ['rx'])
.factory('subjectProxyService', function () {
var subjectProxyService = {};
var subject = new Rx.Subject();
subjectProxyService.subject = function () {
return subject;
}
return subjectProxyService;
})
.factory('appFactory', function(rx) {
function addContextToMessage(query) {
var deferred = $.Deferred();
var timestamp = moment().format("YYYY-MM-DD");
var _msg = "[" + timestamp + "]" + query;
deferred.resolve(_msg);
return rx.Observable
.fromPromise(deferred.promise())
.map(function(contextMsg) {
return contextMsg;
});
}
return {
addContextToMessage: addContextToMessage
};
})
.controller('appCtrl', function($scope, rx, appFactory, subjectProxyService) {
$scope.query = "";
$scope.message = "";
subjectProxyService.subject().subscribe(
function (x) { console.log('Value published to observer: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted');
});
$scope.$createObservableFunction('clickFunction')
.map(function () {
return $scope.query;
})
.flatMapLatest(appFactory.addContextToMessage)
.map(function(results) {
return results;
})
.subscribe(subjectProxyService.subject());
// give the service-subject as the subscriber to the "click stream"
})
You can do pure RxJS inside of Angular for sure, but you can also write mostly jQuery inside your Angular app if you really wanted. So long as all the libraries are loaded you're just running JavaScript inside of Angular. I would consider it mostly best practice and also future-smart to use the Angular extensions, though, which you can also expect to support Angular and RxJS changes. Not to mention, they've taken the time to write a bunch of tested helper and constructor methods you don't have to write for yourself -- like mapping/linking a $watch on a $scope variable to an Observable.
Resources
Here's some more resources for Rx-Angular. There are for instance 3 helpful methods that you can use to create Observables inside of Angular. They're basically helpful constructor methods for RX Observables (so you don't have to write your own!):
$toObservable
$toObservable - $scope variables
$eventToObservable
$eventToObservable - $events
$createObservableFunction
$createObservableFunction - binding functions in scope to an Observable, like ngClick
Using Subjects in RxJS
Subjects AS-A Service
add a comment |
up vote
1
down vote
I put together a small JSFiddle demonstrating a modified example from the RxJS-Angular Extensions Project. EDIT My first example was very similar to the one in the resources for click events. I've made the example a little more complex, and it examples using a Subject to publish and subscribe to data that is handled/modified by a factory that returns a promise.
Versions note: Rx-Angular, as of 09.28.2017, only supports RxJS version 4. RxJS is a dependency to RxAngular, so be careful or you'll run into errors you cannot resolve.
In short, though, my answer is to use the Rx-Angular extensions library. Partly because RxJS is a dependency for Angular-RX, so you'll have it anyway and can use both. But also because it allows for some simple Observable crafting that can be useful, like for events or "$watches".
https://jsfiddle.net/ejmLpyrn/3/
HTML
<div ng-app="myApp" ng-controller="appCtrl">
<!-- RxJS v4.1.0, RxAngular v1.5-->
<div class="jumbotron">
<div class="form-group">
<input type="text" ng-model="query" />
<button type="button" ng-click="clickFunction()">
Search
</button>
</div>
</div>
<hr>
<div>{{message}}</div>
</div>
JS
angular.module('myApp', ['rx'])
.factory('subjectProxyService', function () {
var subjectProxyService = {};
var subject = new Rx.Subject();
subjectProxyService.subject = function () {
return subject;
}
return subjectProxyService;
})
.factory('appFactory', function(rx) {
function addContextToMessage(query) {
var deferred = $.Deferred();
var timestamp = moment().format("YYYY-MM-DD");
var _msg = "[" + timestamp + "]" + query;
deferred.resolve(_msg);
return rx.Observable
.fromPromise(deferred.promise())
.map(function(contextMsg) {
return contextMsg;
});
}
return {
addContextToMessage: addContextToMessage
};
})
.controller('appCtrl', function($scope, rx, appFactory, subjectProxyService) {
$scope.query = "";
$scope.message = "";
subjectProxyService.subject().subscribe(
function (x) { console.log('Value published to observer: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted');
});
$scope.$createObservableFunction('clickFunction')
.map(function () {
return $scope.query;
})
.flatMapLatest(appFactory.addContextToMessage)
.map(function(results) {
return results;
})
.subscribe(subjectProxyService.subject());
// give the service-subject as the subscriber to the "click stream"
})
You can do pure RxJS inside of Angular for sure, but you can also write mostly jQuery inside your Angular app if you really wanted. So long as all the libraries are loaded you're just running JavaScript inside of Angular. I would consider it mostly best practice and also future-smart to use the Angular extensions, though, which you can also expect to support Angular and RxJS changes. Not to mention, they've taken the time to write a bunch of tested helper and constructor methods you don't have to write for yourself -- like mapping/linking a $watch on a $scope variable to an Observable.
Resources
Here's some more resources for Rx-Angular. There are for instance 3 helpful methods that you can use to create Observables inside of Angular. They're basically helpful constructor methods for RX Observables (so you don't have to write your own!):
$toObservable
$toObservable - $scope variables
$eventToObservable
$eventToObservable - $events
$createObservableFunction
$createObservableFunction - binding functions in scope to an Observable, like ngClick
Using Subjects in RxJS
Subjects AS-A Service
add a comment |
up vote
1
down vote
up vote
1
down vote
I put together a small JSFiddle demonstrating a modified example from the RxJS-Angular Extensions Project. EDIT My first example was very similar to the one in the resources for click events. I've made the example a little more complex, and it examples using a Subject to publish and subscribe to data that is handled/modified by a factory that returns a promise.
Versions note: Rx-Angular, as of 09.28.2017, only supports RxJS version 4. RxJS is a dependency to RxAngular, so be careful or you'll run into errors you cannot resolve.
In short, though, my answer is to use the Rx-Angular extensions library. Partly because RxJS is a dependency for Angular-RX, so you'll have it anyway and can use both. But also because it allows for some simple Observable crafting that can be useful, like for events or "$watches".
https://jsfiddle.net/ejmLpyrn/3/
HTML
<div ng-app="myApp" ng-controller="appCtrl">
<!-- RxJS v4.1.0, RxAngular v1.5-->
<div class="jumbotron">
<div class="form-group">
<input type="text" ng-model="query" />
<button type="button" ng-click="clickFunction()">
Search
</button>
</div>
</div>
<hr>
<div>{{message}}</div>
</div>
JS
angular.module('myApp', ['rx'])
.factory('subjectProxyService', function () {
var subjectProxyService = {};
var subject = new Rx.Subject();
subjectProxyService.subject = function () {
return subject;
}
return subjectProxyService;
})
.factory('appFactory', function(rx) {
function addContextToMessage(query) {
var deferred = $.Deferred();
var timestamp = moment().format("YYYY-MM-DD");
var _msg = "[" + timestamp + "]" + query;
deferred.resolve(_msg);
return rx.Observable
.fromPromise(deferred.promise())
.map(function(contextMsg) {
return contextMsg;
});
}
return {
addContextToMessage: addContextToMessage
};
})
.controller('appCtrl', function($scope, rx, appFactory, subjectProxyService) {
$scope.query = "";
$scope.message = "";
subjectProxyService.subject().subscribe(
function (x) { console.log('Value published to observer: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted');
});
$scope.$createObservableFunction('clickFunction')
.map(function () {
return $scope.query;
})
.flatMapLatest(appFactory.addContextToMessage)
.map(function(results) {
return results;
})
.subscribe(subjectProxyService.subject());
// give the service-subject as the subscriber to the "click stream"
})
You can do pure RxJS inside of Angular for sure, but you can also write mostly jQuery inside your Angular app if you really wanted. So long as all the libraries are loaded you're just running JavaScript inside of Angular. I would consider it mostly best practice and also future-smart to use the Angular extensions, though, which you can also expect to support Angular and RxJS changes. Not to mention, they've taken the time to write a bunch of tested helper and constructor methods you don't have to write for yourself -- like mapping/linking a $watch on a $scope variable to an Observable.
Resources
Here's some more resources for Rx-Angular. There are for instance 3 helpful methods that you can use to create Observables inside of Angular. They're basically helpful constructor methods for RX Observables (so you don't have to write your own!):
$toObservable
$toObservable - $scope variables
$eventToObservable
$eventToObservable - $events
$createObservableFunction
$createObservableFunction - binding functions in scope to an Observable, like ngClick
Using Subjects in RxJS
Subjects AS-A Service
I put together a small JSFiddle demonstrating a modified example from the RxJS-Angular Extensions Project. EDIT My first example was very similar to the one in the resources for click events. I've made the example a little more complex, and it examples using a Subject to publish and subscribe to data that is handled/modified by a factory that returns a promise.
Versions note: Rx-Angular, as of 09.28.2017, only supports RxJS version 4. RxJS is a dependency to RxAngular, so be careful or you'll run into errors you cannot resolve.
In short, though, my answer is to use the Rx-Angular extensions library. Partly because RxJS is a dependency for Angular-RX, so you'll have it anyway and can use both. But also because it allows for some simple Observable crafting that can be useful, like for events or "$watches".
https://jsfiddle.net/ejmLpyrn/3/
HTML
<div ng-app="myApp" ng-controller="appCtrl">
<!-- RxJS v4.1.0, RxAngular v1.5-->
<div class="jumbotron">
<div class="form-group">
<input type="text" ng-model="query" />
<button type="button" ng-click="clickFunction()">
Search
</button>
</div>
</div>
<hr>
<div>{{message}}</div>
</div>
JS
angular.module('myApp', ['rx'])
.factory('subjectProxyService', function () {
var subjectProxyService = {};
var subject = new Rx.Subject();
subjectProxyService.subject = function () {
return subject;
}
return subjectProxyService;
})
.factory('appFactory', function(rx) {
function addContextToMessage(query) {
var deferred = $.Deferred();
var timestamp = moment().format("YYYY-MM-DD");
var _msg = "[" + timestamp + "]" + query;
deferred.resolve(_msg);
return rx.Observable
.fromPromise(deferred.promise())
.map(function(contextMsg) {
return contextMsg;
});
}
return {
addContextToMessage: addContextToMessage
};
})
.controller('appCtrl', function($scope, rx, appFactory, subjectProxyService) {
$scope.query = "";
$scope.message = "";
subjectProxyService.subject().subscribe(
function (x) { console.log('Value published to observer: ' + x); },
function (e) { console.log('onError: ' + e.message); },
function () { console.log('onCompleted');
});
$scope.$createObservableFunction('clickFunction')
.map(function () {
return $scope.query;
})
.flatMapLatest(appFactory.addContextToMessage)
.map(function(results) {
return results;
})
.subscribe(subjectProxyService.subject());
// give the service-subject as the subscriber to the "click stream"
})
You can do pure RxJS inside of Angular for sure, but you can also write mostly jQuery inside your Angular app if you really wanted. So long as all the libraries are loaded you're just running JavaScript inside of Angular. I would consider it mostly best practice and also future-smart to use the Angular extensions, though, which you can also expect to support Angular and RxJS changes. Not to mention, they've taken the time to write a bunch of tested helper and constructor methods you don't have to write for yourself -- like mapping/linking a $watch on a $scope variable to an Observable.
Resources
Here's some more resources for Rx-Angular. There are for instance 3 helpful methods that you can use to create Observables inside of Angular. They're basically helpful constructor methods for RX Observables (so you don't have to write your own!):
$toObservable
$toObservable - $scope variables
$eventToObservable
$eventToObservable - $events
$createObservableFunction
$createObservableFunction - binding functions in scope to an Observable, like ngClick
Using Subjects in RxJS
Subjects AS-A Service
edited Sep 29 '17 at 3:27
answered Sep 28 '17 at 17:49
RoboBear
2,03511318
2,03511318
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37629822%2frxjs-and-using-with-angular-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
An explanation of the use case would improve the question, it is too broad in its current state. If it is $scope that is observed, it is done in controller for sure. The official repo has no tutorials, but there are examples and exhaustive API documentation.
– estus
Jun 4 '16 at 15:45
Ok. More specifically then, would I be better using the angular extension for rxjs or using rxjs direct?
– Martin
Jun 4 '16 at 23:37
1
I guess, this depends on your use case, too. Check how much of this is applicable to you. Notice that it is still on its way to support RxJS 5.
– estus
Jun 5 '16 at 6:50