[英]Polymer 1.x: accessing element within dom-if
單擊“名字:James”文本。
我在dom-if中有一個輸入元素。 在某種情況下,我正在使dom-if通過,因此該元素將存在,但是在使dom-if條件通過后,我無法立即訪問輸入元素。
可能是我需要知道dom-if的實際計算時間,以及即使條件變為真也為什么不存在該元素的原因。
<!doctype html>
<head>
<meta charset="utf-8">
<base href="https://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link href="polymer/polymer.html" rel="import">
<link href="iron-form/iron-form.html" rel="import">
<link href="paper-input/paper-input.html" rel="import">
</head>
<body>
<dom-module id="x-element">
<template>
<style></style>
<template is="dom-if" if="{{!editMode}}">
<span id="name" on-tap="_makeEditable">{{name}}: {{value}}</div>
</template>
<template is="dom-if" if="{{editMode}}">
<paper-input id="input" label="{{name}}" value="{{value}}"></paper-input>
</template>
</template>
<script>
(function(){
Polymer({
is: "x-element",
properties: {
editMode: {
type: Boolean,
value: false
},
name: {
type:String,
value:"First Name"
},
value: {
type:String,
value:"James"
}
},
_makeEditable: function() {
this.editMode = true;
//how to find the input element here
//this.$$('#input').querySelector("input").focus();
//this.$.input.querySelector("input").focus();
}
});
})();
</script>
</dom-module>
<x-element></x-element>
</body>
這是因為將this.editMode
設置為true
不會立即更新DOM。
您應該異步地運行選擇器,以便Polymer有一些時間來更新DOM,例如:
this.async(function() {
this.$$('#input').focus();
})
在這里擺弄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.