0

我正在使用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 下拉列表的更改事件中变为真。ui-select但是即使函数返回 true,焦点也不会到达。我已经验证了该指令,它适用于常规的 html 输入。

4

1 回答 1

0

我不确定您是否使用了如下所示的超时。尝试以这种方式将代码包装在超时中。

 function safeFocus() {
          $timeout(function () {
              element[0].focus();
          }, 0);
      }
于 2017-01-23T09:56:19.140 回答