简体   繁体   English

$ scope更改时重新运行反应式助手

[英]Re-run reactive helper when $scope changes

I have a reactive data source in Angular-Meteor and would like to find a way to make the reactive function re-run after I change another of the $scope values. 我在Angular-Meteor中有一个反应式数据源,想在我更改另一个$ scope值后找到一种使反应式函数重新运行的方法。 Here, I want to change the sort order based on $scope.model.sort: 在这里,我想基于$ 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} });
        }
    });

I then change the sort order in the html or by calling a function: 然后,我在html中或通过调用函数来更改排序顺序:

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

JS JS

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

Neither of these ways seems to re-run the reactive helper. 这两种方法似乎都无法重新运行反应性助手。 Is it possible to make the reactive helper re-run when the referenced $scope.model.sort is changed? 更改引用的$scope.model.sort时是否可以使反应性助手重新运行?

maybe you need add getReactively 也许您需要添加getReactively

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

Reading api document for more detail 阅读api 文档以获取更多详细信息

The helper needs to be in a reactive area, right now it does not seem to be so. 助手需要处于反应区域,现在看来并非如此。 You have $scope as a helper but not linked to anything? 您有$ scope作为帮助者,但未链接任何东西? So if you put it in a template helper, it will run whenever scope changes, but as a stand alone helper (as it seems right now) it will not re-run automatically when the data context changes. 因此,如果将其放在模板帮助程序中,它将在范围更改时运行,但是作为独立的帮助程序(如现在所示),它将在数据上下文更改时不会自动重新运行。

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

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