如何 wrap/intercept 一个 Angular 范围值

How to wrap/intercept an Angular scope value

我正在尝试使用 Angular multiselect 指令(具体来说 isteven/angular-multi-select)作为 multi-select 接口组件。它的问题是它 returns 的数据是完整的 selected 对象数组,而我们宁愿使用对象的简化版本,例如 ID 数组。由于这个组件已经被我们自己的抽象指令包装,我想找到一些方法来拦截正在修改的子范围值,然后 return 通过 ngModel 范围减少值 属性 在我的包装器指令上。

指令定义

angular.module('cw-ui').directive('cwSelect', function() {
    return {
        scope: {
            ngModel: '=',
            options: '=',
            maxLabels: '@?',
            selectionMode: '@?',
            onClose: '&'
        },
        templateUrl: 'UI/Directives/select',
        compile: function(element, attributes) {
            if(attributes.maxLabels === undefined) {
                attributes.maxLabels = 3;
            }
        }
    };
});

包装指令模板

<isteven-multi-select input-model="options" output-model="ngModel" button-label="icon name" item-label="icon name maker" tick-property="ticked" group-property="msGroup" max-labels="{{::maxLabels}}" selection-mode="{{selectionMode}}" on-close="onClose()"></isteven-multi-select>

不要完全遵循,但一种不太优雅的方法是监视由 isteven 指令修改的模型值,然后将该数据打包到其他模型对象中以供使用你自己的指令。