【发布时间】:2015-03-04 21:50:19
【问题描述】:
您好,我想在我的文本区域内按 Enter 时提交我的表单。
我的 textarea 有一个在 angularjs 指令中创建的自动完成表单 (autocompleteAngularExpression)
我试过这个:
<textarea ng-keyup="$event.keyCode == 13 && submit()"
id="inputId" autocomplete-angular-expression>
</textaera>
问题是,当我在文本区域的自动完成中按 Enter 时,我提交了表单。
只有在显示自动完成表单时我才能提交表单?
我的指令有点复杂。
在范围内我有未解析的值
directive('autocompleteAngularExpression', ['_', '$', function(_, $) {
function split(val) {
return val.split( /\s+/ );
}
function extractLast(term) {
return term.split(/[()-\s]+/).pop();
}
return {
require: 'ngModel',
scope: {
indexed : "=indexedValue",
nonIndexedValue : "=nonIndexedValue"
},
link: function(scope, element, attrs, ngModel) {
function containsSomeIndexed(words) {
return _.some(words, function(word) {
return _.contains(scope.indexedValue, word);
});
}
ngModel.$parsers.unshift(function(expression) {
if (!expression || expression === "") {
ngModel.$setValidity('indexValid', true);
} else {
ngModel.$setValidity('indexValid', containsSomeIndexed(expression.split(/[()-:\s]+/)));
}
return expression;
});
element.autocomplete({
minLength: 1,
source: function(request, response) {
var sourceList;
if (containsSomeIndexed(request.term.split(/[()-:\s]+/))) {
sourceList = _.union(scope.indexedValue, scope.nonIndexedValue);
} else {
sourceList = scope.indexedValue;
}
response($.ui.autocomplete.filter(sourceList, extractLast(request.term)));
},
focus: function() {
return false;
},
select: function(event, ui) {
var selectedValue = ui.item.value;
var terms = split(this.value);
var partial = terms.pop();
var prependBuffer = "";
while (partial.charAt(0) === '(' || partial.charAt(0) === '-') {
prependBuffer = prependBuffer + partial.charAt(0);
partial = partial.substring(1, partial.length);
}
terms.push(prependBuffer + selectedValue);
return false;
}
});
}
};
}]).
【问题讨论】:
-
您好,您的问题有点令人困惑。如果你可以修改它并明确你的问题是什么以及你想要实现什么,这将有助于人们回答你的问题。
-
你能显示
autocompleteAngularExpression指令的代码吗? -
显示你创建的指令,也许我们可以修改它和 ng-enter 指令来满足你的要求
-
@OrGuz 自动完成指令有点复杂
-
@gregouille 我会尝试在范围上设置一个变量,当打开自动完成列表并将 ng-enter 上的 if 语句更改为 if(e.which === 13 && isAutocompleteOpened === 假)
标签: javascript angularjs forms autocomplete angularjs-directive