Angularjs 安格拉斯。如何将模型传递给自定义xeditable指令?(我正在试图找到从ui选择更新模型的最佳方法)
我有一个指令,允许将ui.select与xeditable集成。这是我的指示:Angularjs 安格拉斯。如何将模型传递给自定义xeditable指令?(我正在试图找到从ui选择更新模型的最佳方法),angularjs,x-editable,ui-select,Angularjs,X Editable,Ui Select,我有一个指令,允许将ui.select与xeditable集成。这是我的指示: var Dashboard = angular.module('Dashboard'); Dashboard.directive('editableUiSelect', ['editableDirectiveFactory', 'editableNgOptionsParser', function(editableDirectiveFactory, editableNgOptionsParser)
var Dashboard = angular.module('Dashboard');
Dashboard.directive('editableUiSelect', ['editableDirectiveFactory', 'editableNgOptionsParser',
function(editableDirectiveFactory, editableNgOptionsParser) {
return editableDirectiveFactory({
directiveName: 'editableUiSelect',
inputTpl: '<span></span>',
scope: {
eNgModel: "="
},
render: function() {
this.parent.render.call(this);
var parsed = editableNgOptionsParser(this.attrs.eNgOptions);
var filter = " | filter: $select.search";
var html = "<ui-select ng-model='eNgModel' theme='bootstrap' style='width: 150px'>"+
"<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);
}
});
}]);
我想动态地将模型传递给我的指令,但似乎xeditable editableDirectiveFactory不允许我这样做。因此,我无法保存从该ui选择的值。如何解决此问题
请看下面我的答案,但我的方法只是难看的变通方法。我需要有人能告诉我解决这个问题的最佳方法。目前我通过在ui select的on select属性中添加setModel方法解决了这个问题。 这样,我的html变量变为:
var html = "<ui-select ng-model='"+this.name+"' theme='bootstrap' style='width: 150px' on-select='setModel($item)'>"+
"<ui-select-match><span ng-bind='$select.selected.name'></span></ui-select-match>"+
"<ui-select-choices repeat='"+parsed.ngRepeat+"'>"+"<span ng-bind='"+parsed.locals.displayFn+"'></span>"+
"</ui-select-choices></ui-select>";
最后,我在save方法中得到了$scope.objectX值
这种方法很难解决问题,所以我真的需要一个人,他能告诉我最好的方法。我有一点进步。在我的html变量中,我将ng model='eNgModel'替换为ng model='+this.name+',现在当字段变得可编辑时,它们包含当前模型值。但模型仍然无法保存。我继续调试。不幸的是,当您将HTML作为数据处理并将其存储在JavaScript变量中时,您将只有一些难看的解决方法。@Claies谢谢您的评论。我以xeditable.js的源代码为例,这种方法在它自己的指令中使用。不幸地但我认为,您是对的,所以在最近的时间内,我将尝试将所有html标记从js移动到单独的.html文件中。
$scope.setModel = function(item) {
if (item.hasOwnProperty('property')) {
$scope.object1 = item;
} else {
$scope.object2 = item;
}
}