javascript - 有 Angular 。如何使用 xeditable 将 ui.select 注入(inject)指令?

标签 javascript angularjs x-editable ui-select

我正在使用 xeditable 和 ui.select。我需要配置选择和搜索才能使用 xeditable。所以,我创建了指令:

var xeditable = angular.module('xeditable');

xeditable.directive('editableUiSelect', ['editableDirectiveFactory', 'editableNgOptionsParser',
        function(editableDirectiveFactory, editableNgOptionsParser) {
            return editableDirectiveFactory({
                directiveName: 'editableUiSelect',
                inputTpl: '<span></span>',
                render: function() {
                    this.parent.render.call(this);
                    var parsed = editableNgOptionsParser(this.attrs.eNgOptions);
                    var filter = " | filter: $select.search";
                    var html = "<ui-select ng-model='"+parsed.ngModel+"'>"+ 
                        "<ui-select-match>"+ "<span ng-bind='"+$select.selected.name+"'></span></ui-select-match>"+
                        "<ui-select-choices repeat='"+parsed.ngRepeat+filter+"'>"+"<span ng-bind='"+parsed.locals.displayFn+"'></span>"+
                        "</ui-select-choices></ui-select>";

                    this.inputEl.removeAttr('ng-model');
                    this.inputEl.removeAttr('ng-options');
                    this.inputEl.html(html);
                },
                autosubmit: function() {
                    // do smth
                }
            });
        }]);

这不起作用,因为找不到 $select。 $selectuiSelectCtrl Controller ,在 uiSelect 指令中使用。

看来,我需要将 ui.select 注入(inject)到我的指令中,但我不知道如何做到这一点并保持当前的 xeditable 实例。

问题:如何将 ui.select 或仅 $select Controller 注入(inject)到我的指令中?

如果我提供的详细信息不够,请告诉我。

已解决。我根据 @jusopi 答案更改了指令代码,并将 $select$parent.data 放入引号中,全部变成工作:

var Dashboard = angular.module('Dashboard');

Dashboard.directive('editableUiSelect', ['editableDirectiveFactory', 'editableNgOptionsParser',
        function(editableDirectiveFactory, editableNgOptionsParser) {
            return editableDirectiveFactory({
                directiveName: 'editableUiSelect',
                inputTpl: '<span></span>',
                render: function() {
                    this.parent.render.call(this);
                    var parsed = editableNgOptionsParser(this.attrs.eNgOptions);
                    var filter = " | filter: $select.search";
                    var html = "<ui-select ng-model='$parent.data'>"+ 
                        "<ui-select-match>"+ "<span ng-bind='$select.selected.name'></span></ui-select-match>"+
                        "<ui-select-choices repeat='"+parsed.ngRepeat+filter+"'>"+"<span ng-bind='"+parsed.locals.displayFn+"'></span>"+
                        "</ui-select-choices></ui-select>";

                    this.inputEl.removeAttr('ng-model');
                    this.inputEl.removeAttr('ng-options');
                    this.inputEl.html(html);
                },
                autosubmit: function() {
                    // do smth
                }
            });
        }]);

最佳答案

这假设您的问题是因为您没有正确注入(inject)模块依赖项(正如 @uday 所指出的)。

据我所知,您不会/不能向现有模块添加额外的模块依赖项。以下内容将覆盖外部库定义的xeditable模块,因为此语法定义模块(而不是使用模块来定义其他可注入(inject)构造)。

angular.module( <name>, <other-modules-array> )

相反,您需要在自己的模块中定义指令,确保引用您的 xeditableui.select 模块,如下所示:

angular.module( 'myApp', [ 'xeditable', 'ui.select' ])
.directive( 'editableUiSelect', [ 
    'editableDirectiveFactory', 
    'editableNgOptionsParser',
    function( editableDirectiveFactory, editableNgOptionsParser ) { 
        ...
    }
])

关于javascript - 有 Angular 。如何使用 xeditable 将 ui.select 注入(inject)指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34683861/

相关文章:

javascript - 如何从不在 'change' 处理程序中的 kendoDatePicker 获取当前日期

javascript - Angular $http无法发送内容类型 header

javascript - jsPDF 未在 Edge 浏览器上渲染 highcharts 图像

javascript - Angularfire $loaded().then(从这里返回一些东西)

angularjs - 当绑定(bind)到嵌套 JSON 对象时,无法让 Angular-xeditable Editable-Select 选择当前项目

ajax - x-editable ajax调用不处理指定的url

javascript - angular.js 中文本框的唯一数字

javascript - 如何在我的 d3js 粒子模拟器中添加碰撞检测

javascript - 如何发出 PUT API HTTPS 请求 [reactJS]

javascript - X-editable 防止输入框在聚焦时关闭的默认行为