Knock out visible binding of anchor tag is not working

半世苍凉 提交于 2019-12-14 04:26:32

问题


I am using JavaScript module pattern in my application. Here is my html binding

<a href="#" data-bind="visible:master.child.showDeleteLink,click: function(obj, event) { master.child.showDeletePopup() } ">

My view modal is

    master.child=(function (my, jQuery, ko) {
    var textTemp;
    my.ViewModel = function () {
    self.showDeleteLink = ko.observable();
        self.showDeleteLink = function () {
            if (textTemp.length > 500)
                return true;
            else
                return false;
       }
    ko.applyBindings(my.anothermodule);
    } 
}(master.child, $, ko));

Click binding, html are text are working pretty fine :).

But visiblity binding is not working.any body have any idea? am i wrong any where?


回答1:


my.ViewModel = function () {
    self.showDeleteLink = ko.observable();
        self.showDeleteLink = function () {
            if (textTemp.length > 500)
                return true;
            else
                return false;
       }

your "self.showDeleteLink" is no longer observable. You give it a new value, being the function.

I think what you are looking for is a computed value: http://knockoutjs.com/documentation/computedObservables.html

self.showDeleteLink = ko.computed(function() {
        return (textTemp.length > 500);
    });



回答2:


change The binding to data-bind="visible:master.child.showDeleteLink() now its working pretty fine



来源:https://stackoverflow.com/questions/16058730/knock-out-visible-binding-of-anchor-tag-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!