淘汰父和根绑定

时间:2013-12-08 13:13:38

标签: knockout.js knockout-2.0 knockout-mvc knockout-sortable

我遇到父和根绑定问题。

function SignCell() {
         var self = this;
         self.MathSign = ko.observable("+");
         self.AdditionCell = ko.observable("");
     }     
     function Expression(cell) {
         var self = this;
         self.BaseCell = ko.observable(cell);
         self.AddCell = ko.observableArray([]);
     }

 function Condition(active, expression1, sign, expression2, errMassage) {
     var self = this;
     self.Active = ko.observable(active);
     self.Expression1 = expression1;
     self.Sign = ko.observable(sign);
     self.Expression2 = expression2;
     self.ErrMassage = ko.observable(errMassage);
 }
 var ViewModel = function() {
     var self = this;
     self.ConditionArray = ko.observableArray([
         new Condition(true,new Expression(""),"=",new Expression(""),"")
     ]);     
     self.RemoveExpr1 = function () {
         var i = self.ConditionArray.indexOf(arr);
         self.ConditionArray()[i].Expression1.AddCell.push(new Expression());
     };         
 };

和html

<tbody data-bind="foreach: ConditionArray">
    <tr>
      <td><input class="input-small" data-bind="value: Expression1.BaseCell"/> 
             <table>
                <tbody  data-bind="foreach: Expression1.AddCell">
                    <tr>                          
                            <button data-bind="click: $parent.RemoveExpr1" class="btn btn-danger"> <i class="icon-minus-sign icon-white"></i></button>
                        </td>
                    </tr>
                </tbody>
            </table>

我无法使用点击:$ parent.RemoveExpr1“调用 self.RemoveExpr1 功能 你有任何想法如何解决这个问题

1 个答案:

答案 0 :(得分:1)

使用$root.RemoveExpr1代替$parent.RemoveExpr1

您在嵌套循环中。您想要的功能附加到ViewModel。此处$root指的是ViewModel而非$parent