gpt4 book ai didi

javascript - AngularJS 中的纯功能 Controller

转载 作者:行者123 更新时间:2023-11-29 14:47:06 25 4
gpt4 key购买 nike

是否可以只使用纯函数在 AngularJS 中编写 Controller ?我无法理解将状态保存在何处以及如何以纯函数方式对其进行操作。

我正在使用 Angular 主页上的待办事项应用程序。我能做的最好的事情就是分离出纯部分并在 Controller 方法中调用它们。

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);
}
}

注意:我正在进行一项可行性研究,以了解是否可以将纯函数式技术用于 AngularJS。

最佳答案

似乎答案已经很晚了。尽管如此,我还是会尝试。使用 Angular 1, Controller 作为 100% 纯函数是不可能的,因为 controller 是为了扩充 $scope (即使你使用 controllerAs 语法) 而这个 $scope 是 MVVM 模式的关键。 Angular View 是此 $scope 的副作用。

你可以像你所做的那样抽象到一定程度,但仅此而已。仅此而已。

关于javascript - AngularJS 中的纯功能 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31468310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com