如何通过控制器中的谓词代码调用 ng-repeat 排序?

How to call ng-repeat sort by predicate code from Controller?

我有 2 个指令,一个控制标签列表的视图并包含几个排序按钮,另一个是具有相同选项的弹出菜单:

tagPanel 的标记:

<header>
    <ul>
        <li ng-class="{'active':vol}"
            ng-click="vol = true; predicate = 'week_quantity'; reverse=!reverse">Vol</li>
        <li ng-class="{'active':!vol}"
            ng-click="vol = false; predicate = 'previous_day_quantity'; reverse=!reverse">Trend</li>
        <li><div class="quick-filter-ico" ng-click="toggleTagPopover()"></div></li>
        <li><div class="quick-search-ico"></div></li>
    </ul>
</header>

<section>
    <ul>
        <li ng-repeat="(k, m) in tags | orderBy:predicate:reverse"
            ng-class="{'selected': m.selected}"
            ng-click="selectTag(m)">
            <div class="tag">{{m.term}}</div>
        </li>
    </ul>
</section>

以下是孤立的分类:

<li ng-class="{'active':vol}"
    ng-click="vol = true;
    predicate = 'week_quantity'; reverse=!reverse">Vol</li>

<li ng-class="{'active':!vol}"
    ng-click="vol = false;
    predicate = 'previous_day_quantity'; reverse=!reverse">Trend</li>

tagsPanel 控制器中的代码:

var vs = $scope;
ApiFactory.getTags(ticker).then(function(data) {

    // The tags model is set here:
    vs.tags = TagFactory.renderTags(ticker, data);

现在我的目标是 toggle/activate 来自另一个指令控制器的谓词代码 tagsPopover.

这是来自 tagsPopover 的代码:

var vs = $scope;

vs.toggleTags = function(type) {
    console.log(type);
    vs.tagsSort = {};

    switch(type) {
        case 'vol':
            vs.tagsSort.vol = true;
            // Here I need to call a function in tagsPanel
            // that will toggle the predicate code
            break;

        case 'trending':
            vs.tagsSort.trending = true;
            // Here I need to call a function in tagsPanel
            // that will toggle the predicate code
            break;
    }

    vs.tagsPopoverDisplay = false;
};

所以我的问题是,如何从指令/控制器内的标记切换谓词代码?

predicate = 'previous_day_quantity'; reverse=!reverse

我将在中调用的函数类型示例 tagsPanel:

vs.togglePredicate = function() {
    predicate = 'previous_day_quantity';
    reverse = !reverse;
};

代码与标记完全一样,只需要使用我的 $scope 变量附加到它:

tagsPopover:

vs.toggleTags = function(type) {
    vs.tagsSort = {};
    tagsPanel   = ScopeFactory.getScope('tagsPanel'); 

    switch(type) {
        case 'all':
            vs.tagsSort.all = true;
            tagsPanel.togglePredicate('vol');
            break;

        case 'trending':
            vs.tagsSort.trending = true;
            tagsPanel.togglePredicate('trending');
            break;
    }

    vs.tagsPopoverDisplay = false;
};

然后在tagsPanel:

vs.togglePredicate = function(type) {

    console.log('togglePredicate called...');
    console.log(type);

    switch(type) {
        case 'vol':
            vs.predicate = 'week_quantity';
            vs.reverse   = !vs.reverse;
            break;

        case 'trending':
            vs.predicate = 'previous_day_quantity';
            vs.reverse   = !vs.reverse;

            console.log(vs.predicate);
            break;
    }
};