基于此stackoverflow问题:How to set focus on input field?
我在我的示例中使用了名为 <div id= "show">
<video width="320" height="240" src="file:///F:/test.mp4" type="video/mp4" controls>
</video>
</div>
的相同指令focuseMe
:http://plnkr.co/edit/tdSxya?p=preview
但是,一旦显示输入,输入就不会自动获得焦点。
focusSearch
按钮Show Search
AngularJS代码:
<!-- <div class="search-input container-fluid"> -->
<div class="search-input" ng-show="showingSearchInput">
<input type="text"
class="form-control"
placeholder="Search"
ng-model="vh.searchInput"
ng-click="vh.searchPop($event)"
focus-search="showingSearchInput">
<div class="close-x-sml" ng-click="hideSearch()">(x)</div>
</div>
答案 0 :(得分:2)
那是因为当您的指令名称是focusSearch时,您正在解析focusMe的值。将$ parse参数更改为attrs.focusSearch,您的指令工作
var model = $parse(attrs.focusSearch);
答案 1 :(得分:0)
刚刚意识到他最近更新了他的原始问题并更新了他的指令。这现在有效:
https://stackoverflow.com/a/14837021/168738
app.directive('focusMe', function($timeout) {
return {
scope: { trigger: '=focusMe' },
link: function(scope, element) {
scope.$watch('trigger', function(value) {
if(value === true) {
//console.log('trigger',value);
//$timeout(function() {
element[0].focus();
scope.trigger = false;
//});
}
});
}
};
});