Knockout:无法处理绑定

2023-12-14

我之前问过这个问题,但没有得到答案。

当我运行我的代码时收到此错误消息:

Uncaught ReferenceError: Unable to process binding "visible: function (){return !editable() }"
Message: editable is not defined 

可编辑功能应该切换真/假,然后在按下按钮时切换到编辑模式。这个按钮是通过 html 中的 foreach 调用的,所以我猜测它与我的视图模型有关。我从 getJson 获得的输出工作正常,但可编辑功能以某种方式发生冲突。

这是我的 html 代码:

<div><ul data-bind="foreach: comments">
  <li class="ul3">
     <span class="author" data-bind="text: nickname, visible: !editable(), click: editComment">
    </span>
     <input type="text" data-bind="value: nickname, visible: editable()"/>:
     <div>  

     <span class="comment" data-bind="text: newMsg, visible: !editable(), click: editComment">    
     </span>
     <textarea class="myComment" type="text" data-bind="value: newMsg, visible: editable()">                       
    </textarea>

    </div>
     <button data-bind="click: editComment, text: editable() ? 'Save' : 'Edit comment'">           
     </button> 
     <button data-bind="click: deleteComment">Delete</button>
          </li>
       </ul>
    </div>

这是我的 JavaScript:

      function Comment() {
    var self = this;
    self.nickname = ko.observable();
    self.newMsg = ko.observable();
    self.editable = ko.observable(false);

    self.sendEntry = function () {
     vm.selectedComment(new Comment());

        if (self.newMsg() !== "" && self.nickname() !== "") {

            $.post(writeUrl, "entry=" + ko.toJSON(self));
            self.newMsg("");
        }
        vm.cSection().getNewEntries();
    };
    self.deleteComment = function () {
        vm.comments.remove(self);
    };

     self.editComment = function () {
        self.editable(!self.editable());
    };
}
function commentSection() {
    var self = this;
    self.timestamp = 0;
     var entry;
    self.getNewEntries = function () {

        $.getJSON(readUrl, "timestamp=" + self.timestamp, function (comments) {
            for (var i = 0; i < comments.length; i++) {
                 entry = comments[i];
                if (entry.timestamp > self.timestamp) {
                    self.timestamp = entry.timestamp;
                }
                vm.comments.unshift(entry);
            }
             self.getNewEntries();
        });
    };

}

function ViewModel(){
    var self = this;

    self.cSection=ko.observable(new commentSection());
    self.comments = ko.observableArray();
    self.selectedComment = ko.observable(new Comment());

    //self.cSection().getNewEntries();
}
var vm=new ViewModel();
ko.applyBindings(vm);
vm.cSection().getNewEntries();

});

我用你的代码做了一些东西,现在切换工作正常。

请找到这个工作小提琴

View :

<input type="button"
    data-bind="click: editComment, value:editable() ? 'Save' : 'Edit comment'" /> 

查看型号:

$(document).ready(function() {
    vm = function ViewModel() {
        var self = this;
        self.comments = ko.observableArray();
        function Comment() {
            var self=this;
            self.editable = ko.observable(false);
            self.editComment = function() {
                self.editable(!self.editable());
            };
        }
        self.comments.push(new Comment());  
    };
    ko.applyBindings(new vm);
});

如果问题仍然存在,请使用上面的小提琴并尝试在其中构建您的代码,让我知道。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Knockout:无法处理绑定 的相关文章

随机推荐