Thursday, June 13, 2019

how to solve the TypeError: is not a function in knockout JS?

when we implement an web app with knockout JS, we encounter an issue in the page load.

we create an observable object to store an object with observable properties.

self.MyObservable=ko.observable();

self.myObject=new MyObject(){
      property1: ko.observable(),
      property2: ko.observable(),
      property3: ko.observable()
}
self.MyObservable(self.myObject);

in the method.. we have the logic to check the property1 value

then we have to access the property1 with the following code

self.MyObservable().property1()

unfortunately we encounter TypeError:property1 is not a function in chrome debugger

here is the work around to fix the error.

if (ko.isObservable(self.MyObservable().property1)) {
                        return self.MyObservable().property1();

  }




No comments:

Post a Comment