淘汰ajax foreach循环不工作

时间:2013-08-22 13:38:45

标签: javascript ajax jquery knockout.js

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

 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/

1 个答案:

答案 0 :(得分: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"}]')