2016-12-02 31 views
4

我有以下問題:我想專注於一個輸入。但我有一個微調控件在頁面加載時被觸發,並且只有在某些http請求完成時纔會隱藏。此外,所有的輸入都被禁用,直到請求沒有完成。所以它看起來像這樣:焦點HTML元素啓用後

<fieldset ng-disabled="!noPendingRequests"> 
    ... 
    <input id="input-to-focus-on" uib-datepicker-popup="MM/dd/yyyy" type="text" ng-model="startDate" ... /> 
    <spinner ng-hide="noPendingRequests"></spinner> 
</fieldset> 

Spinner是一個自定義的Angular指令。有要求自動對焦#input-to-focus-on輸入,但它不起作用。在所有的http請求都使用$q.all解析後,我試圖集中注意力。但這並不起作用。我想這是因爲在啓用輸入之前觸發$q中的回調。我也試過這樣的手動啓用:

$q.all(somePromises).then(function() { 
    angular.element('#input-to-focus-on')[0].disabled = false; 
    angular.element('#input-to-focus-on')[0].tabIndex = 1; 
    angular.element('#input-to-focus-on').focus(); 
}); 

我不知道該怎麼做,並希望有任何幫助。在此先感謝,夥計們!

+2

值得一試[ngFocus指令(https://docs.angularjs.org/api/ng/directive/ngFocus),因爲它是建立在角流。 – GillesC

+0

@GillesC感謝您的建議,但遺憾的是它不工作太 –

+0

@GillesC這個怎麼樣一個https://gist.github.com/zbicin/2b3fdf538936287c1a96?爲了達到這個目的,我前一段時間已經做了。 –

回答

2

嘗試在$timeout塊包裝的焦點通話。

$timeout(function() { 
    angular.element('#input-to-focus-on').focus(); 
}); 
+0

它的工作原理!它真的有用,男人!我無法表達你是多麼高興你來到這裏並寫下這個答案 –

2

試試這個:

$q.all(somePromises).then(function() { 
 
    $timeout(function() { 
 
     angular.element('#input-to-focus-on')[0].focus(); 
 
    }); 
 
});

此外,請確保您有注入到你的控制器$超時。