简体   繁体   English

AngularJS中的纯功能控制器

[英]Purely Functional Controllers in AngularJS

Is it possible to use only pure functions to write controllers in AngularJS? 是否可以仅使用纯函数在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. 我正在使用Angular主页上的todo应用程序。 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. 注意:我正在进行可行性研究,以了解是否可以使用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. 使用Angular 1,控制器作为100%纯函数是不可能的,因为controller旨在增加$scope (即使你使用controllerAs语法),这个$scope是MVVM模式的关键。 And angular view is the side-effect of this $scope . 角度视图是这个$scope的副作用。

You can abstract as you did up to certain level but that's it. 你可以抽象到某种程度,但就是这样。 Nothing more than that. 没有比这更好的了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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