knockout.js - KnockoutJS - observable object is undefined? -
I'm still new to knockout so I'm hoping someone can help me on this.
This is my visual model:
baseModel = {logged_in: ko.observable (), alert: ko.observableArray (), Errors: ko.observable ()} ; And in my HTML I have:
& lt; Span data-bind = "text: ($ root.errors ['myerror1'] == $ Undefined): $ root.errors ['myerror1']" & gt; & Lt; / Span & gt; I am hoping to drag arbitrary items through AJAX and specify it to me. Errors for property For example, when my response is:
object {myerror1: "password is incorrect", error 2: "something else is wrong"} < P> I expect to show an error in my span when I do the following in my AJAX success callback: if (feedback errors! == undefined ) BaseModel.errors = Ko.mapping.fromJS (Reaction Errors, {}, Besodel. Errs); OR,
if (feedback. Errors! == undefined) baseModel.errors = response. Errs; Neither myerror1 shows that I'm bound in HTML, maybe I was doing many things wrong here. I'm not sure what's wrong thanks!
If you are trying to update errors new value, You should not set it using = , but it must be given the new value as the parameter: baseModel.errors ({myerror1: "password incorrect Is ", error 2:" something else is wrong "}); Update: I have seen two additional problems with your code: -
Errors Start as Undefined ; Set this up better in a blank object, so there will be no error when the first evaluation of the binding occurs: Errors: To .hosable ({}) -
If you want a value inside an expression in the errors to the data-band , you get the value of "call" Instead of using it directly: & lt; Span data-bind = "text: ($ root.errors () ['myerror1'] === undefined)? 'Undefined': $ root .errors () ['myerror1']" & gt; & Lt; / Span & gt; .
Comments
Post a Comment