jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – Knockout:无法处理绑定大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我早些时候问了这个问题,但没有得到答案.

我运行代码时收到此@L_450_1@消息:

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

可编辑的函数应该切换为true / false,然后在按下按钮时切换到编辑模式.这个按钮是通过html中的foreach调用的,所以我猜它与我的viewmodel有关.我从我的getJson得到的输出工作正常,但可编辑的函数以某种方式冲突.

这是我的HTML代码

<>
  • @H_675_29@

    这是我的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();
    
    });
    
    最佳答案
    我现在从你的代码做了一些切换工作正常.

    请找到这个Working Fiddle

    查看:

    查看型号:

    $(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);
    });
    

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

    大佬总结

    以上是大佬教程为你收集整理的javascript – Knockout:无法处理绑定全部内容,希望文章能够帮你解决javascript – Knockout:无法处理绑定所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。