点击时我有一个链接,它必须显示另一个输入和自动对焦。
<a ng-click="ctrl.switchAndFocus()" href="#">FOCUS</a>
在控制器中我有
this.switchAndFocus= function (){
if (this.controler.data.radioOne == 'one') {
this.controler.data.radioTwo = "kmh";
angular.element("#high").focus();
}
else{
this.controler.data.radioTwo = "kmh";
angular.element("#low").focus();
}
}
(this.controler.data.radioOne是单选按钮,值为&#39; one&#39;,this.controler.data.radioTwo是raduibutton,值为&#39; kmh&#39;,angular.element(& #34;#high&#34;) - &gt;来自输入字段的id)
这有效,但我必须点击链接2次。任何想法如何让它工作1点击切换和聚焦?
答案 0 :(得分:1)
https://github.com/hiebj/ng-focus-if
http://plnkr.co/edit/MJS3zRk079Mu72o5A9l6?p=preview
<input focus-if />
(function() {
'use strict';
angular
.module('focus-if', [])
.directive('focusIf', focusIf);
function focusIf($timeout) {
function link($scope, $element, $attrs) {
var dom = $element[0];
if ($attrs.focusIf) {
$scope.$watch($attrs.focusIf, focus);
} else {
focus(true);
}
function focus(condition) {
if (condition) {
$timeout(function() {
dom.focus();
}, $scope.$eval($attrs.focusDelay) || 0);
}
}
}
return {
restrict: 'A',
link: link
};
}
})();