Bob*_*ing 2 focus angularjs angularjs-scope angularjs-ng-click
我正在尝试构建一个 Angular JS 表单。我希望用户能够在单击按钮时将焦点设置在文本字段上。不知道为什么这不起作用?谢谢
html:
<div ng-app="" ng-controller="personController">
<p>Name: <input type="text" ng-model="name" id="question1" focus="{{focusThis}}"></p>
<p ng-bind="name"></p>
<input type="button" value="focus" ng-click="focus()">
</div>
Run Code Online (Sandbox Code Playgroud)
角JS功能:
function personController($scope)
{$scope.focus=function(){
$scope.focusThis="true";
};
};
Run Code Online (Sandbox Code Playgroud)
一些通用解决方案如何($是jQuery):
mod.directive('nsFocusId', ['$timeout', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs, ctrl) {
element.click(function() {
$timeout(function () { $('#' + attrs.nsFocusId).focus(); }, 0);
});
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
用法:
<label data-ns-focus-id="id-of-target-element">a</label>
Run Code Online (Sandbox Code Playgroud)
该指令可用于任何元素,它将聚焦id您提供的元素。
我曾经$timeout只是为了让它在未来的升级中更加灵活(但可以随意用scope.$apply函数来包装它);
| 归档时间: |
|
| 查看次数: |
42948 次 |
| 最近记录: |