javascript - 扩展 UI 选择 Controller ( Angular )

标签 javascript angularjs inheritance decorator angular-ui-select

我正在尝试扩展 ui-select(版本 0.17.1)以使其在点击时刷新。这里有一个示例 ( https://jsfiddle.net/betonetotbo/0yfhe7pf/),我一直将其用作起点。

我希望在整个应用程序范围内发生这种情况,因此我将 ui-select-choices 指令 html 替换为我自己的指令 html,其中包含一个 div 按钮。我还添加了一个装饰器,用一个尚未定义的新 Controller 替换指令 Controller 。

myapp.app.config(function(provide) {
    $provide.decorator("uiSelectDirective", [ "$delegate", "uiSelectConfig", "$controller"], function ($delegate, uiSelectConfig, $controller) {
        var directive = $delegate[0];
        // This line throws error Unknown provider
        var $select = $controller('uiSelectCtrl');

        return $delegate;
    }]);
});

我的问题是我还想装饰/扩展 Controller 以在整个应用程序中添加额外的功能。我不想完整地重写它,所以我正在寻找扩展这样的 Controller 的正确方法。

谢谢。

最佳答案

我通过重载指令的编译方法解决了这个问题:

$provide.decorator('uiSelectDirective', ['$delegate', function ($delegate) {
    var directive = $delegate[0];
    var compile = directive.compile;

    directive.compile = function (tElement, tAttrs) {
        var link = compile.apply(this, arguments);
        return function (scope, element, attrs, ngModel) {
            link.apply(this, arguments);
            var ctrl = ngModel[0];
            var activate = ctrl.activate;

            //I had to close the dropdown when clicking on the directive
            ctrl.activate = function (initSearchValue, avoidReset) {
                if (!ctrl.disabled) {
                    if (ctrl.open) {
                        ctrl.close();
                    } else {
                        activate(initSearchValue, avoidReset);
                    }
                }
            }
        };
    };

    return $delegate;
}]);

关于javascript - 扩展 UI 选择 Controller ( Angular ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39328815/

相关文章:

javascript - 如何确定我的 ajax 请求是在客户端还是服务器端失败?

javascript - 防止取消选择 xtype :checkbox based on condition in ExtJS 5. 1

javascript - 使用 mvc 模型在 Google Maps API v3 上显示标记

javascript - 更改所有具有 id= 值的元素

javascript - AngularJS - Uncaught Error : [$injector:nomod]

Java 继承 - 当对派生对象使用基变量时,基类方法被派生类覆盖

jquery - 通过填充先前本地存储的值的字段来更改 Angular 模型

angularjs - 在 AngularJS 中使用包含括号的名称验证输入

c++ - 继承 c++ 子父

c# - C# 3 中具有接口(interface)继承(co(ntra)-variance?)的泛型类型推断