我有一个简单的指令,专注于表单的特定输入字段 它只是失败:元素没有集中。 这是指令代码:
angular.module('myApp').directive('autoFocus', function($timeout) {
return {
scope : {
trigger: '@autoFocus'
},
link: function(scope, element) {
scope.$watch('trigger', function(value) {
if (value === 'true') {
$timeout(function() {
console.log('giving focus to element', element[0].id);
element[0].focus();
});
}
});
}
};
});
这是标记:
<form ng-show="signedIn()" class="service form-horizontal">
....
<div class="row" title="Author">
<div class="col-xs-8>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" class="form-control" ng-model="service.author" auto-focus="true" placeholder="" ng-disabled="servicePrintMode" />
</div>
</div>
</div>
....
</form>
知道它不起作用的原因吗?
我甚至接受一个解决方法......: - )
更新 :
这是jsfiddle:在Chrome上它就像魅力一样,在Firefox(30.0)上它没有...: - (
答案 0 :(得分:1)
angular.module('myApp').directive('autoFocus', function ($timeout) {
return {
scope: {
trigger: '@autoFocus'
},
link: function (scope, element) {
scope.$watch('trigger', function (value) {
console.log('focus to element ' + element[0].id + " " +value );
if (value == 'true') {
$timeout(function () {
console.log('giving focus to element', element[0].id);
element[0].focus();
});
}
});
}
};
});