我正在使用ui-select
的角实现。它工作良好,但element[0].focus()
不与它一起工作。为了进一步解释它,我有以下指令,将重点放在ui-select
var focusIf = function ($timeout, $parse) {
return {
require: "ngModel",
//scope: {
// updateFrom: "&"
//},
link: function (scope, element, attrs, ngModel) {
console.log("in focus if");
var focusChecker = $parse(attrs.focusIf);
scope.$watch(function () {
return focusChecker(scope);
}, function (newVal, oldVal) {
console.log("new val", newVal);
console.log("old val", oldVal);
if (newVal === true && !oldVal) {
console.log('inside condition');
console.log("element", element[0]);
$timeout(function () {
element[0].focus();
});
}
})
}
};
};
app.directive("focusIf", focusIf);
我在ui-select
上使用此指令,如
<ui-select name="supplierId" data-focus-if="isSupplierInvoice()" ng-model="transaction.SupplierInvoice.supplierId" theme="select2" data-ng-show="isSupplierInvoice()" data-ng-required="isSupplierInvoice()" data-ng-disabled="disabled" style="width:100%;">
<ui-select-match placeholder="Select a supplier or search...">{{$select.selected.Name}}</ui-select-match>
<ui-select-choices repeat="supplier.ID as supplier in suppliers | propsFilter: {Name: $select.search, IdentityNumber: $select.search}">
<div ng-bind-html="supplier.Name | highlight: $select.search"></div>
<small>
Identity Number: <span ng-bind-html="''+supplier.IdentityNumber | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>
isSupplierInvoice()
在常规html下拉列表的更改事件时变为true。但焦点没有得到ui-select
,即使函数返回true。我已经验证了指令,它可以正常工作与常规的html输入。
我不确定您是否像下面这样使用了超时。尝试用这种方式在超时中包装代码。
function safeFocus() {
$timeout(function () {
element[0].focus();
}, 0);
}