[英]Adding a global variable / function in JavaScript (specifically NativeScript)
[英]Bound variable not updating in function in Nativescript (JavaScript)
我有一个页面,可以使用点击按钮时调用的功能将listview
切换为可见/隐藏。
listview
元素的visibility
属性设置为
{{ showFixtures ? 'visible' : 'collapsed' }}
当我最初绑定页面时, showFixtures
变量设置为false
且listview
被隐藏。 反转后,将listview
。 这证明了showFixtures
变量是最初绑定的。
轻按按钮时,应该将showFixtures
变量在true
和false
之间切换。 我可以点击按钮并调用该函数,然后查看页面变量是否已更改,但是listview
元素不会变为可见/隐藏(取决于上下文)。
page.xml
<Page loaded="loaded">
<!-- ... -->
<Label text="Toggle Fixtures" ontap="toggleFixtures" />
<ListView items="{{ fixturesList }}" visibility="{{ showFixtures ? 'visible' : 'collapsed' }}">
<ListView.item>
<GridLayout class="list-matches-ko" rows="30" columns="auto,*">
<Label row="0" col="0" class="list-matches-ko-date" text="{{ koLabelDate }}" />
<Label row="0" col="1" class="list-matches-ko-time" text="{{ koLabelTime }}" />
</GridLayout>
</ListView.item>
</ListView>
<!-- ... -->
</Page>
page.js
var observableModule = require("data/observable");
var pageData = new observableModule.Observable;
exports.loaded = loaded;
function loaded(args){
page = args.object;
pageData.showFixtures = false;
page.bindingContext = pageData;
}
//...
exports.toggleFixtures = toggleFixtures;
function toggleFixtures(args){
pageData.showFixtures = pageData.showFixtures ? false : true;
console.log(args.object.bindingContext.showFixtures);
console.log(page.bindingContext.showFixtures);
console.log(pageData.showFixtures);
}
轻按三次按钮后,控制台上的输出为:
JS: true
JS: true
JS: true
JS: false
JS: false
JS: false
JS: true
JS: true
JS: true
如何通过使用绑定变量使用此函数使listView
元素可见/隐藏,为什么它不按现在的方式工作?
由于您使用的是可观察的普通本机脚本,请尝试以下操作
pagedata.set('showFixtures', YOUR TERNARY HERE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.