元素[0] .focus无法使用角度ui-select

时间:2015-05-17 09:26:22

标签: angularjs angularjs-directive ui-select

我正在使用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。但是,即使函数返回true,焦点也不会到达ui-select。我已经验证了该指令,它可以正常使用html输入。

1 个答案:

答案 0 :(得分:0)

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

 function safeFocus() {
          $timeout(function () {
              element[0].focus();
          }, 0);
      }