简体   繁体   中英

Purely Functional Controllers in AngularJS

Is it possible to use only pure functions to write controllers in AngularJS? I am unable to get my head around where to keep the state and how to manipulate it in a purely functional way.

I am working with the todo application on the Angular home page. The best I could do was separate out the pure 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 understand if it is possible to use purely functional techniques with AngularJS.

Seems answer is very late. Nevertheless I will still attempt. With Angular 1, controller as a 100% pure function is not possible because controller is meant to augment $scope (even if you use controllerAs syntax) and this $scope is the crux of MVVM pattern. And angular view is the side-effect of this $scope .

You can abstract as you did up to certain level but that's it. Nothing more than that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM