Angular和javascript的新手,已经让我的大脑陷入困境2天了。我有一个html表,我需要显示和隐藏复选框。每行有5个复选框。可见的内容取决于FieldName和FieldName数组中的索引。我已经对下面的代码进行了评论,我希望它能够解释我尝试过的内容。
html页面上的角度代码如下所示,数字范围从0到4 - ng-show =" ShowCheckbox(info.FieldName,0)"
javascript看起来像 -
$scope.ShowCheckbox = function (FieldName, index) {
// FieldName is "MandatedMaterials", "AgencyAgmt", etc.
var MandatedMaterials = [true, true, false, false, false];
var AgencyAgmt = [true, true, false, false, false];
return FieldName[index];
// This is what I really really want to work.
// Only MandatedMaterials[2] and AgencyAgmt[3] return false
// My experiments to narrow down the problem.
//if (FieldName == "MandatedMaterials")
// works, so FieldName is coming from index.cshtml
//if (index == 3)
// works, same thing as above
//if (MandatedMaterials[0] == true)
// works, in that it showed all checkboxes
//if (AgencyAgmt[0] == true)
// works, same as above
//if (FieldName[0] == true)
// NOPE -- should show all checkboxes, but instead none
//if (FieldName[0])
// works, in that it shows all checkboxes
// For testing purposes I had "if" code to return true or false for the
// commented out statements above. This meant those bits of code would
// show all the checkboxes.
}
如果你能帮助我显示和隐藏相应的复选框,我将非常感激。感谢。
答案 0 :(得分:0)
我不确定这会有所帮助,但这有点像你想要的。我认为你可以用它作为你想要做的事情的基础。
http://www.w3schools.com/angular/tryit.asp?filename=try_ng_events_hide
答案 1 :(得分:0)
你不能传入一个字符串,然后使用is作为一个数组。
如果你传入ShowCheckBox("AgencyAgmt", 3)
,
事实上,你返回" AgencyAgmt"的第四个字符,你返回' n'
所以你的检查失败了。
你应该这样定义:
var chkBoxValues = {};
chkBoxValues.MandatedMaterials = [true, true, false, false, false];
chkBoxValues.AgencyAgmt = [true, true, false, false, false];
return chkBoxValues[fieldName];
这将有效。