Polymers iron-ajax要素で初めて作業していて、解決するのがかなり簡単だと思う問題に出くわしましたが、解決策を見つけることができませんオンライン。
iron-ajaxリクエストから返された値を変数に格納する(ポリマー)
私はユーザーの詳細をiron-ajax要素で取得し、dom-repeatでループします。ユーザーの名字は、ユーザーを表示するために作成したカスタムカード要素に配置されています。返されたid値を変数に格納することができないことを除いて、すべて正常に動作します。 これはデータバインディングの使用で可能だと確信していますが、動作させることができませんでした。 JavaScriptでgetAttributeを使って値を取得しようとしても、nullが返されます。私の鉄のAjaxリクエスト&レスポンス以下のコードで
は、id値を取得するために私の厄介な試みを含む見つけることができます{{item.id}}。実行するには、私は上のタップ機能を作成したときに、ユーザーがクリックstdId属性の値を取得する必要があるカードの1つに追加します。
<dom-module id="my-students-view">
<template>
<style>
:host {
display: block;
}
</style>
<!-- Iron-Ajax created connection and gets data -->
<iron-ajax
auto
id="requestRepos"
url="http://api.dev/user/"
handle-as="json"
loading="{{isloading}}"
last-response="{{response}}"></iron-ajax>
<!-- dom-repeat iterates the response -->
<template is="dom-repeat" items="[[response.data]]" sort="sortData">
<student-card
id="studentCard"
to="http://localhost:8080/profile-view/{{item.id}}"
picsrc="../../images/anonymous.jpg"
name="{{item.first_name}} {{item.last_name}}"
stdId="{{item.id}}"
on-tap="sendId"></student-card>
</template>
</template>
<script>
Polymer({
is: 'my-students-view',
properties: {
stdId: {
notify: true,
}
},
//Sort the array/data alphabetically
sortData: function(a, b) {
if(a.first_name < b.first_name) return -1;
if(a.first_name > b.first_name) return 1;
return 0;
},
//Try to get the id value of a the student card
sendId: function() {
var idPropertie = this.$$("#studentCard");
var idValue = idPropertie.getAttribute('stdId');
console.log('the id is:' + idValue);
},
});
</script>
</dom-module>
どうもありがとう@Patricklafを! なぜ私は event.modelを通じてアクセスできるのかを説明する文書はありますか? – Niklas
ドキュメントはこちら:https://www.polymer-project.org/1.0/docs/devguide/templates#handling-events – Patricklaf