0
votes

I have used knockout if binding to show/hide a div based on value from model.

var model = {
        // Model-----------------------------------------------
        router: router,
        chosen: chosen,
        selectedItems: [],
        selectedItemsList: [],
        p: {
            Id: null,
            IsValid : false
        },

         activate: function (id) {
            var self = this;
            var apiRoute = 'test/id';
            return api.GetModel(apiRoute, null, self);
         },
         approve: function ()
         {
            return self.activate(1);
         }
 };

 <!-- ko if: p.IsValid -->
    <div></div>
<!-- /ko -->

This works when the application is loaded first time. But to refresh the view I am calling self.activate() in a button click. After calling self.activate(), the show/hide functionality stops working.

Can someone help me identify what is the issue?

2
You need to share more code. - GôTô
var model = { // Model----------------------------------------------- router: router, chosen: chosen, selectedItems: [], selectedItemsList: [], p: { Id: null, IsValid : false }, activate: function (id) { var self = this; var apiRoute = 'test/id'; return api.GetModel(apiRoute, null, self); }, approve: function () { return self.activate(1); } }; <!-- ko if: p.IsValid --> <div></div> <!-- /ko --> - VJ1243
You have an edit link at the bottom left of your question - GôTô
Hmmm.. You don't seem to use observables here - GôTô
For what reason would you need to reactivate the view to refresh it? - Brett

2 Answers

0
votes

You can just use the "visible" binding

in the viewModel

   viewModel ={
     activate: activate,
     title: 'Bewertung',
     attached: attached,
     someVal : ko.observable(true)
   }


  return viewModel

in the view

 <div data-bind="visible: someVal"></div>

to change the visibility you can access "someVal"like this viewModel.someVal(false) from attached for example

0
votes

Try this:

viewModel ={
 activate: activate,
 title: 'Bewertung',
 attached: attached,
 someVal : ko.observable(true)

}

<div data-bind="visible: someVal()"></div>