javascript - Angular js validation function returns before the api call for validation is completed -
if (!validate()) { return; }; function validate() { var nameisvalid = false; var validationstatus = true; dataapi.getnamevalid(viewmodel.name).then(function (data) { nameisvalid = data.isvalid; validationstatus = checkvalidinput(nameisvalid); }); return validationstatus; } function checkvalidinput(isvalid) { if(!isvalid){ return false; } //chk other input field validations }
the issue validate() returns true.if change validationstatus = false returns false. getnamevalid async false still return validationstatus called before getnamevalid() completes execution checkvalidinput never gets called , actual validation never happens. there approach kind of scenarios need wait ajax call before function returns. article or blog pointing in right direction help. thanks
i think can chain onto 1 have there , want. granted untested think should work.
if (!validate()) { return; } function validate() { var nameisvalid = false; var validationstatus = true; dataapi.getnamevalid(viewmodel.name) .then(function (data) { nameisvalid = data.isvalid; validationstatus = checkvalidinput(nameisvalid); }).then(function(validationstatus){ return validationstatus; }); } function checkvalidinput(isvalid) { if(!isvalid) { return false; } }
Comments
Post a Comment