我最近想要显示一个特定属性(如果已定义),如果未定义,我想显示一个带有一些指令的DIV元素。
但我无法理解这一点;我已尝试使用$root
甚至绑定属性,但没有成功。
以下是我的代码示例:
function InvestigatorInfo() {
var self = this;
self.Name = ko.observable();
self.Description = ko.observable();
self.TypeName = ko.observable();
self.AssemblyName = ko.observable();
self.ResultType = ko.observable();
self.EnumTypeName = ko.observable();
self.Obsolete = ko.observable();
self.InvestigatorType = ko.observable();
self.Properties = ko.observable();
self.Requires = ko.observable();
}
InvestigatorInfo.prototype.fromJS = function(data) {
var self = this;
self.Name(data.Name || "");
self.Description(data.Description || "");
self.TypeName(data.TypeName || "");
self.AssemblyName(data.AssemblyName || "");
self.ResultType(data.ResultType || "");
self.EnumTypeName(data.EnumTypeName || "");
self.Obsolete(data.Obsolete || "");
self.InvestigatorType(data.InvestigatorType || "");
self.Properties(data.Properties.Properties || []);
self.Requires(data.Requires.Interfaces || []);
}
我的索引:
<ul class="collapsible" data-collapsible="accordion" data-bind="foreach:InvestigatorInfos">
<li>
<div class="collapsible-header">
<i class="material-icons">view_quilt</i>
<p class="blue-text" data-bind="text: TypeName"></p>
</div>
<div class="collapsible-body">
<p class="style_p_row" ><b>AssemblyName:</b> <span data-bind="text: AssemblyName"></span></p>
<!-- ko if:Description -->
<p class="style_p_row" ><b>Description:</b> <span data-bind="text: Description"></span></p>
<!-- /ko -->
<!-- ko if:EnumTypeName -->
<p class="style_p_row" >
<b>EnumTypeName: </b><span data-bind="text: EnumTypeName"></span></p>
<!-- /ko -->
<p class="style_p_row" >
<b>InvestigatorType:</b> <span class="investigatortype-class" data-bind="text:investigatorName[InvestigatorType]"></span></p>
<p class="style_p_row" ><b>Name: </b><span data-bind="text: Name"></span></p>
<!-- ko if:Obsolete -->
<p class="style_p_row" ><b>Obsolete: </b><span data-bind="text: Obsolete"></span></p>
<!-- /ko -->
<p class="style_p_row" ><b>TypeName: </b><span data-bind="text: TypeName"></span></p>
<!-- ko if:ResultType -->
<p class="style_p_row" ><b>ResultType: </b><span data-bind="text: resultName[ResultType]"></span></p>
<!-- /ko -->
<!-- ko ifnot:Properties-->
<p class="blue-text padding_p">Properties</p>
nothing is set
<!-- /ko -->
<!-- ko if: Properties-->
<table class="bordered">
<thead>
<tr>
<th data-field="Name">Name</th>
<th data-field="Converter">Converter</th>
<th data-field="ValidationName">ValidationName</th>
<th data-field="EnumTypeName">EnumTypeName</th>
</tr>
</thead>
<tbody data-bind="foreach: Properties">
<tr>
<td><span data-bind="text: Name"></span>
</td>
</tr>
</tbody>
</table>
<!-- /ko -->
</div>
</li>
</ul>
Properties
中的ko if: Properties
部分始终显示,即使其后面的数组中没有值,也不会显示ko ifnot: Properties
中定义的元素。
JSON数据:
{
"BrickInfos": {
"BrickInfos": [
{
"Properties": {
"Properties": [
]
},
"Implements": {
"Interfaces": [
{
"TypeName": "ITSR2.Bricks.Access.IAccessBareBoneBrick"
},
{
"TypeName": "ITSR2.Bricks.Access.IAccessAppBrick"
}
]
},
"Name": "AccessBareBoneApp",
"Description": "",
"TypeName": "ITSR2.Bricks.Access.AccessBareBoneApp",
"AssemblyName": "ITSR2.Bricks.MSOffice, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null",
"Obsolete": false
},
{
"Properties": {
"Properties": [
{
"Name": "MainFile",
"Description": "",
"ValidationType": 4,
"Converter": 8,
"EnumTypeName": ""
}
]
},
"Implements": {
"Interfaces": [
{
"TypeName": "ITSR2.Bricks.Access.IAccessBrick"
},
{
"TypeName": "ITSR2.Bricks.Access.IAccessAppBrick"
}
]
},
"Name": "AccessFile",
"Description": "",
"TypeName": "ITSR2.Bricks.Access.AccessFile",
"AssemblyName": "ITSR2.Bricks.MSOffice, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null",
"Obsolete": false
},
]
}
}
答案 0 :(得分:1)
您的Properties
属性是observable
,看起来像是一个数组,并且设置为data.Properties.Properties
或空数组([]
)。
[]
是"truthy" value,因此即使代码已将[]
分配给Properties
,淘汰赛if
绑定也会将其视为已填充。你需要检查它的长度:
<!-- ko if:Properties().length == 0-->
注意()
- 我们需要调用observable来获取底层数组 - observable本身没有length
属性。
关于您的更新,以Obsolete
为例,您已将其传递给false
。这意味着:
<!-- ko if: Obsolete -->
永远不会显示它的内容,因为它显然false
。您需要将其定制为您期望的值 - 因为您将默认值设置为""
,您可以检查:
<!-- ko ifnot: Obsolete() === "" -->
因此,如果它特别是一个空白字符串,它将无法显示,将显示任何其他内容。您的每个属性都需要进行类似的检查,具体取决于您传递给他们的内容以及是否有任何&#34;有效&#34;您想要显示的值最终可能会被视为虚假值。