Purely functional controllers in AngularJS
Is it possible to only use pure functions to write controllers in AngularJS? I can't figure out where to store the state and how to manipulate it in a purely functional way.
I am working with a todo app on the Angular homepage. The best I could do is highlight the clean parts and call them in the controller methods.
var _remaining = R.compose(R.length, R.filter(R.prop('done')));
var _archive = R.filter(R.compose(R.not, R.prop('done')));
class TodoListCtrl {
constructor() {
this.todos = [
{text: 'learn angular', done: true},
{text: 'build an angular app', done: false}];
this.todoText = '';
}
remaining() {
return _remaining(this.todos);
}
archive() {
this.todos = _archive(this.todos);
}
}
Note. I am doing a feasibility study to see if purely functional methods can be used with AngularJS.
source to share
The answer seems to be very late. Nevertheless, I will try anyway. When using Angular 1, the controller as a 100% clean feature is not possible as it is controller
meant to grow $scope
(even if you use the syntax controllerAs
) and this $scope
is the highlight of the MVVM pattern. And Angular's view is a side effect of that $scope
.
You can abstract as you did up to a certain level, but that's it. No more.
source to share