自动对焦不工作当用于NG-包括

问题描述:

我想将焦点设置在局部视图输入框之一
等等。

I want to set focus to one of the input box in partial view like .

这是工作的罚款时,页面加载的第一次。但是当我改变谐音自动对焦无法正常工作。

This is working fine when page loads for the first time. but when I change the partials autofocus doesn't work .

我相信这是因为在页面加载自动对焦工作,怎么能在这里做的工作

I believe it is because of autofocus work on pageload how can it make work here

我不知道如何解决页面重载的问题,但我认为,我们可以在这里工作了另一种解决方案。我有条件地设置聚焦写了一个小指令一旦输入元素。

I am not sure how to fix the problem of page reload but I think we can work out another solution here. I wrote a small directive once to conditionally set focus on an input element.

下面是code:

function SetFocusDirective($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var model = $parse(attrs.setFocus);
            scope.$watch(model, function (value) {
                if (value === true) {
                    element[0].focus();
                }
            });
            element.bind('blur', function () {
                scope.$apply(model.assign(scope, false));
            });
        }
    };
}

SetFocusDirective.$inject = ['$parse'];

app.directive('setFocus', SetFocusDirective);

和这里是你如何使用它:

And here is how you can use it:

<input type="text" ng-model="firstName" set-focus="autofocusFirstName">

其中, autofocusFirstName 是应该有一个布尔值 $范围变量。

Where autofocusFirstName is a $scope variable which should have a boolean value.

所以每次你的谐音负荷,所有在他们里面的指令会得到链接,做他们的工作。如果你最终使用这个指令,你应该能够实现你想要什么。

So every time your partials load, all the directives inside them will get linked and do their jobs. If you end up using this directive, you should be able to achieve what you want.