0

我正在尝试将一组数据绑定到我的淘汰赛模板,但是当我尝试绑定它时,我得到了

 Uncaught ReferenceError: Unable to parse bindings.
 Bindings value: foreach: attributes
 Message: attributes is not defined

下面是我将用来取回数据的 ajax 调用。我已将 self.attributes 替换为要返回的预期数据,以使其在 jsfiddle 中可测试。

$.ajax({
    url: '/api/MyApi',
    type: 'GET',
    dataType: 'json',
    data: {
        DamageId: urlCat
    },
    success:
           function (data) {
               ko.applyBindings(new DetailViewModel(currentDamage, fakeHistData, data));

           },
    error: function () {
    }
});


function DetailViewModel(data, damHist,attrs) {
var self = this;
self.details = data;
self.attributes  = '{[{"Id": 258,"Value": "Yes","AttributeId": 195,"AttributeName": "FurtherDamage","AttributeText": "Is the condition causing further damage to the property?"},{"Id": 259,"Value": "Zombie Attack","AttributeId": 196,"AttributeName": "Description","AttributeText": "Enter a description for the damage"}]}'
self.damHistory = ko.observableArray(damHist);
}

这是我的 HTML

<div class="itemAttributesContainer topBorder">
    <h4>Attributes</h4>
    <table id="attributeTable" data-bind="foreach: attributes">
        <tr>
            <td>
                <span class="attrQuestion" data-bind="text: AttributeText + ': '" />
                <span data-bind="text: Value" />
            </td>
        </tr>
    </table>
</div>

http://jsfiddle.net/2Tp4E/

4

1 回答 1

0

您的小提琴无论如何都无法正常工作,请通过添加此代码进行检查。为此,您需要淘汰映射插件。

Html:

    <table id="attributeTable" data-bind="foreach: attributes">
            <tr>
                <td>
                    <span data-bind="text: AttributeText() + ': '" />
                    <span data-bind="text: Value()" />
                </td>
            </tr>
        </table>

查看型号:

    self.attributes = ko.mapping.fromJSON('[{"Id": 258,"Value": "Yes","AttributeId": 195,"AttributeName": "FurtherDamage","AttributeText": "Is the condition causing further damage to the property?"},{"Id": 259,"Value": "Zombie Attack","AttributeId": 196,"AttributeName": "Description","AttributeText": "Enter a description for the damage"}]')
于 2013-08-22T14:07:05.960 回答