当 $scope 改变时重新 运行 响应式助手

Re-run reactive helper when $scope changes

我在 Angular-Meteor 中有一个反应式数据源,我想找到一种方法在我更改另一个 $scope 值后使反应式函数重新 运行。在这里,我想根据 $scope.model.sort:

更改排序顺序
angular.module('...').controller('MyCtrl', [...], function(...){

    //Subscribe
    $scope.subscribe('articles');

    //Init
    $scope.model = {
        sort: 1,
        ...
    }

    //Reactive definitions
    $scope.helpers({
        articles: () => {
            console.log("+ articles");
            return Articles.find({...}, {sort: {colToSort: $scope.model.sort} });
        }
    });

然后我在 html 中或通过调用函数更改排序顺序:

ng-click="model.sort=-1"
ng-click="setSort(-1)"

JS

    $scope.setSort = function(sort) {
        console.log("+ setResourcesSort(%s)", sort);
        $scope.model.sort = sort;
    }

这两种方式似乎都无法重新运行 反应式助手。当引用的 $scope.model.sort 更改时,是否可以使反应性助手重新 运行 ?

助手需要在反应区,现在好像不是这样。你有 $scope 作为助手但没有链接到任何东西?所以如果你把它放在一个模板助手中,它会 运行 只要范围发生变化,但作为一个独立的助手(现在看起来)它不会在数据上下文改变时自动重新 运行 .

也许您需要添加 getReactively

  $scope.model = {
        sort: 1,
        ...
    }

    //Reactive definitions
    $scope.helpers({
        articles: () => {
            console.log("+ articles");
            return Articles.find({...}, {sort: {colToSort: $scope.getReactively('model.sort')} });
        }
    });

正在阅读 api document 了解更多详情