我正在使用 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。 $select
是 uiSelectCtrl
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> )
相反,您需要在自己的模块中定义指令,确保引用您的 xeditable
和 ui.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/