角度形式 - 元素焦点
Angular formly - Element focus
我有一个字段包装器,当鼠标悬停时将普通文本转换为输入,我还有一个目录,当它打开时,将焦点放在该字段上。
现在,只要我专注于该字段,我希望该字段保持打开状态,并在焦点变为假时转换回普通文本。有没有好方法可以做到这一点?
我有一个我目前得到的垃圾箱。
包装纸:
template: [
'<div ng-hide="to.editorEnabled" >',
'<div ng-mouseover="to.editorEnabled=true">',
'{{to.label}}</br>',
'{{to.value}}',
'</div>',
'</div>',
'<div focus-me="to.editorEnabled" ng-show="to.editorEnabled">',
'<formly-transclude></formly-transclude>',
'</div>'
].join(' ')
对焦指令:
app.directive('focusMe', function($timeout, $parse) {
return {
link: function(scope, element, attrs) {
var model = $parse(attrs.focusMe);
scope.$watch(model, function(value) {
console.log('value=',value);
if(value === true) {
$timeout(function() {
element[0].firstElementChild.focus();
});
}
});
}
};
});
形态字段:
key: 'textField',
type: 'input',
templateOptions: {
label: 'Text',
type: 'text',
value:vm.model.textField
},
watcher: {
listener: function(field, newValue, oldValue, scope, stopWatching) {
if(newValue) {
if(!newValue || newValue === "") {
field.templateOptions.value = "Undefined";
}
else {
field.templateOptions.value = newValue;
}
}
}
}
}];
在形式上,您可以在模板选项中添加onBlur和onFocus。因此,您可以添加一个名为 focused
的变量,onBlur 将其设置为 false
,onFocus 将其设置为 true
。
templateOptions: {
onBlur:'to.focused=false',
onFocus:'to.focused=true',
focused:'true'
},
在指令中添加了两个作用域变量model
和fucus
。model
是你to.editorEnabled
调用的变量,focus
将是新创建的变量to.focused
。
scope: {
model:'=',
focus:'='
}
稍后在您的指令中,您可以为 focused
变量添加一个额外的观察程序。 scope.$watch("focus", function(value) {
if(!scope.focus){
scope.model=false;
}
});
因此,如果焦点丢失,它将scope.model
设置为 false,从而使输入消失。
还做了一个JS垃圾桶,所以你可以仔细看看它。
希望这就是你要找的。
相关文章:
- 单击时将焦点更改为元素
- 检查元素是如何获得焦点的
- 单击其他元素或鼠标向上时隐藏输入框,但保留一次焦点
- 将焦点移动到页面元素
- 如何使元素在按下按钮时不会失去焦点
- 将焦点设置为输入元素角度 js
- 遍历 DOM 以查找哪个元素具有焦点
- 如何仅在输入元素具有焦点时才启动 setInterval
- jQuery网站的键盘快捷键,焦点使用左键和右键转到上一个或下一个元素
- 如何检查焦点是否离开了一组元素
- 键盘没有在Android和BlackBerry中显示元素焦点
- 焦点和一个元素应该反映到CSS中的另一个元素
- 如何在iOS中对输入元素使用Jquery:焦点选择器
- EaselJS / canvas在拖放过程中更改元素焦点
- 使用javascript / jquery更改元素焦点(单击)上的输入/文本区域样式表
- 角度形式 - 元素焦点
- 防止元素焦点丢失(需要跨浏览器解决方案)
- 全局元素焦点监听器
- 在Angularjs中,作用域值在表单的元素焦点上重置
- 通过JS选择伪元素:焦点和:活动