すみれ!私はこの質問をしています。なぜなら、これらの質問は正しく答えることができないからです。Polymerでブール値属性をバインドする方法はありますか?
In Polymer 1.0 how can I databind to a boolean property of an element?
Polymer 1.x: How to data bind to a variable boolean attribute?
How to bind paper-toggle-button to a Boolean in Polymer
私は、ポリマー中のブール属性をバインドするにはどうすればよいですか?
私はこの要素を持っています。つまり、ユーザーがログインしている場合は「ログイン」属性を反映し、ユーザーが認証されていない場合は属性を設定しません。
<dom-module id="my-oauth">
<template>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'my-oauth',
ready : function() {
this.verifyLogin();
},
properties : {
loggedIn : {
type : Boolean,
reflectToAttribute: true,
value : false
}
},
observers : ['verifyLogin()'],
verifyLogin: function() {
var val = localStorage.getItem("token") === null
|| localStorage.getItem('token_expiration_time') == null
|| localStorage.getItem('token_type') == null
|| localStorage.getItem('token_expiration_created_date') == null
|| !isTokenValid(localStorage.getItem('token_expiration_time'),
localStorage.getItem('token_expiration_created_date'))
? false : true;
if (val) {
this.setAttribute('logged-in',true);
} else {
this.removeAttribute('logged-in');
}
},
logout : function() {
localStorage.removeItem("token");
console.log("Logged out!");
this.verifyLogin();
},
storeLocal(token, expiration, tokenType, issued) {
issued = typeof issued !== 'undefined' ? issued : Date.now();
localStorage.setItem("token", token);
localStorage.setItem("token_expiration_time", expiration);
localStorage.setItem("token_type", tokenType);
//The Time in which was set.
localStorage.setItem("token_expiration_created_date", issued);
this.verifyLogin();
}
});
})();
function receiveToken(token) {
console.log("Token Received.");
console.log(token);
}
</script>
</dom-module>
「ログイン」属性はどのように読み取ることができますか?例えば
:
<my-oauth logged-in$="[[authenticated]]"></my-oauth>
<template is="dom-if" if="[[!authenticated]]">
<h1>User is not logged in</h1>
</template>
<template is="dom-if" if="[[authenticated]]">
<h1>User has logged in</h1>
</template>
私も両方の質問から何をしようとしました:これの
<my-oauth id="js-ouath" {{loggedIn}}></my-oauth>
<template is="dom-if" if="{{!loggedIn}}">
<h1>User is not logged in</h1>
</template>
<template is="dom-if" if="{{loggedIn}}">
<h1>User has logged in</h1>
</template>
なしに動作しません。 Notifierは完璧に動作しています。私は何が欠けていますか?
ありがとうございました。通知をtrueに設定しようとしましたが、要素はそれをバインドしません。実際、コンソールでdocument.querySelectorを実行すると、中括弧{{}}が何かにマップされていないことがわかります。 –
待ち!わかった!あなたは正しかった。したがって、notifyプロパティをtrueに設定すると、Polymerはname-of-attr = "true"を実行するので、単にname-of-attr = "{{myBind}}"を実行する必要があります。 OMG、ありがとう。 –