我有一个这样的可观察数组:
arrayName = ko.observableArray([
{name: First Name, type: normal},
{name: Second Name, type: low},
{name: Third Name, type: high}]);
并希望将我的DOM元素类设置为:
<div data-bind="foreach: arrayName">
<div data-bind="text: name"> </div>
<div data-bind="css: type"> </div>
</div>
有谁知道如何使它成为可能?
答案 0 :(得分:0)
您的示例有效,只需要为每个属性添加字符串引号:
<div data-bind="foreach: arrayName">
<div data-bind="text: name"> </div>
<div data-bind="css: type"> </div>
</div>
<script>
function vm (){
this.arrayName = ko.observableArray([
{name: "First Name", type: "normal"},
{name: "Second Name", type: "low"},
{name: "Third Name", type: "high" }]);
}
ko.applyBindings(new vm());
</script>
答案 1 :(得分:0)
尝试使用它:
HTML:
<div data-bind="foreach: arrayName">
<div data-bind="text: name, css: type"> </div>
</div>
SCRIPT:
var myModel = function() {
this.arrayName = ko.observableArray([
{name: "First Name", type: "normal"},
{name: "Second Name", type: "low"},
{name: "Third Name", type: "high"}]);
}
ko.applyBindings(new myModel());
首先,所有类的值都应该以字符串表示形式定义。
您将类(在您的示例中)添加到空div中,该div没有任何内容且您无法看到它,如果我理解您正确,您应该将类添加到同一div中你添加名称属性。
HTH。