[英]How to change value of an array in knockout.js?
<div class="row">
<div class="col-md-6" data-bind="visible: application.candidateMostRecentAcademic" style="display:none;">
<span>Education:</span>
<!-- ko if: application.candidateMostRecentAcademic != null -->
<!-- ko with: application.candidateMostRecentAcademic[0] -->
<span data-bind="text: $data.degreeTypeName"></span>
<span data-bind="text: $data.institutionName"></span>
<span data-bind="text: $data.dateToYear"></span>
<!-- /ko -->
<!-- /ko -->
</div>
</div>
I was going to make the the column visible only if application.candidateMostRecentAcademic (array) exists. 我打算仅在application.candidateMostRecentAcademic(array)存在的情况下使该列可见。
But I also wanted to add a condition for an array that has 0 length (which is not null). 但我也想为长度为0(不为null)的数组添加条件。
So when I tried to do, 所以当我尝试做的时候
<div class="col-md-6" data-bind="visible: application.candidateMostRecentAcademic.length != 0" style="display:none;">
it gave me an error saying that it cannot access the "length" of a null object. 它给我一个错误,说它无法访问空对象的“长度”。 So, what I am trying to do is, I want to set an array of length 0 to null so that it can just be invisible just like the null object.
所以,我想做的是,我想将长度为0的数组设置为null,以便像null对象一样可以不可见。
How can I do this with knockout data-binding? 如何使用剔除数据绑定做到这一点?
If I understand you correctly, you want your if
statement to pass if the Array is not null
and if it's length
is not equal to 0
? 如果我对您的理解正确,那么您希望您的
if
语句在Array不为null
且length
不等于0
吗?
If this is the case, you could do this in your template: 如果是这种情况,可以在模板中执行以下操作:
<div class="col-md-6" data-bind="visible: application.candidateMostRecentAcademic && application.candidateMostRecentAcademic.length !== 0">
Add another member in viewmodel called isColumnVisible and use this member to show and hide column. 在名为isColumnVisible的视图模型中添加另一个成员,并使用该成员显示和隐藏列。
var viewModel={
isColumnVisible=true;
loadData:function(){
// code to load data;
// application= something
if(application.candidateMostRecentAcademic ===null){
this.isColumnVisible=false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.