简体   繁体   English

在 Knockout.js 中的 foreach 内绑定不起作用

[英]Binding not working inside foreach in Knockout.js

I have to iterate over a dynamic array and build a form and validate it.我必须遍历一个动态数组并构建一个表单并验证它。 I do define the error property but I get an error saying "object doesn't support property or method 'error' and this is triggered by the markup, I believe. I have a bunch of other, non array properties that are validated in the same manner no issues, I am only have a binding error problem with the array one.我确实定义了错误属性,但我收到一条错误消息,说"object doesn't support property or method 'error' ,我相信这是由标记触发的。我有一堆其他的非数组属性在同样的方式没有问题,我只有一个数组的绑定错误问题。

so in my html I have所以在我的 html 我有

  <tbody data-bind="foreach: methToChange.nameTranslations">
            <tr>
                <td><div class="method-lb-1"><label data-bind="text:LangName"></label></div></td>
                <td><div class="method-left">
                    <input type="text" class="input-xl" maxlength="40" placeholder="required"
                            onblur="validateNameTranslations()" 
                            data-bind="value:Translation, css: { 'methods-border-error':  $parent.error() }" />
                     </div>
                 </td>
            </tr>
        </tbody>

in js I have, the below model在我的js中,下面的model

nameTranslations is an array of objects {LangName, Translation} nameTranslations 是一个对象数组 {LangName, Translation}

methToChange: {
        //many other properties
        nameTranslations: ko.observableArray([])
}, 

And later on I init the value and create the error observable, then update the array稍后我初始化值并创建可观察的错误,然后更新数组

init: function() {
  model.methToChange.nameTranslations.error = ko.observable(false);
}

update: function() {
       var model = model.methToChange;

       model.nameTranslations(item.NameTranslations);
},

css: { 'methods-border-error':  $parent.error() }" this part causes an issue and throws an error saying that error property is undefined. 

The definitions for nameTranslations array are placed in the same order as for the other non-array fields, my hunch is that I am not invoking this correctly inside foreach . nameTranslations数组的定义与其他非数组字段的顺序相同,我的直觉是我没有在foreach中正确调用它。

Assuming I have the basic structure correct, I think the issue is that $parent actually relates to the parent of methToChange and not the parent of nameTranslations .假设我的基本结构正确,我认为问题在于 $parent 实际上与methToChange nameTranslations父级有关。

 var model = { methToChange: { //many other properties nameTranslations: ko.observableArray([{ LangName: 'Talaxion', Translation: 'Klingon', NameTranslations: 'something' }]) }, init: function() { model.methToChange.nameTranslations.error = ko.observable(false); }, update: function(item) { var model = model.methToChange; model.nameTranslations(item.NameTranslations); }, validateNameTranslations: function(item) { model.methToChange.nameTranslations.error(true); return false; } } model.init(); ko.applyBindings(model);
 .methods-border-error{ border-color: red }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <table> <tbody data-bind="foreach: methToChange.nameTranslations"> <tr> <td> <div class="method-lb-1"><label data-bind="text:LangName"></label></div> </td> <td> <div class="method-left"> <input type="text" class="input-xl" maxlength="40" placeholder="required" data-bind="value:Translation, css: { 'methods-border-error': $parent.methToChange.nameTranslations.error }, event: {blur: $parent.validateNameTranslations}" /> </div> </td> </tr> </tbody> </table>

I ended up defining a var called ntError on the main level and used $root.ntError() to call it.我最终在主级别定义了一个名为 ntError 的 var,并使用$root.ntError()来调用它。 Nothing else worked inside the loop循环内没有其他工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM