私はこの角度コードを持っています:
<div class="element-wrapper" ng-repeat="element in elements">
<div class="first-wrapper">
<div class="button" ng-click="doSomething(element,$event)">{{element.name}}</div>
</div>
<div class="second-wrapper">
<input type="text" value="{{element.value}}">
</div>
</div>
私がしたいこと:ユーザーがボタンをクリックすると、入力要素がフォーカスされます。
ボタン要素をクリックしてフォーカスした後、入力要素を見つけるにはどうすればよいですか?
次のような関数を実行できます。
function doSomething(element,$event) {
//option A - start manipulating in the dark:
$event.srcElement.parentNode.childNodes[1]
//option B - wrapping it with jQuery:
$($event.srcElement).closest('.element-wrapper').find('input').focus();
}
どちらも機能しません-それを行うためのより良いAngularの方法はありますか? jQuery で.closest()
やasなどの関数を使用していますか?.find()
アップデート:
このハックが機能していることがわかりました(ただし、それでも正しい解決策とは思えません):
function doSomething(element,$event) {
setTimeout(function(){
$($event.srcElement).closest('.element-wrapper').find('input').focus();
},0)
}
私はそれを setTimeout でラップしているので、Angular がすべての操作を終了した後、input 要素に焦点を合わせます。