I need to change the value of the this.usernamevalid variable in the scope using a function called checkusername , this function is launched from the view as follows:
register.jade:
(ng-controller = "controller as txt")
input(type="text", ng-model="formData.username",ng-blur="txt.checkusername('username',formData.username);" )
and checkusername function :
regController.js
ngApp.controller('controller', Main );
function Main(){
this.usernamevalid = false;
this.checkusername = function(param, val) {
if (val != undefined) {
$http.get('http://localhost:3000/users?param='+param+'&val='+val)
.then(function(response){
var size = response.data.length;
switch (param) {
case 'username':
if (size>0) {
this.usernamevalid = true;
} else {
this.usernamevalid = false;
}
break;
default:
console.log('Field undefined, STOP');
}
}, function(response){
alert(response + "(error)");
});
}
}
}
I am trying to use the callback function, but the result was the same, I can not change the result of this.usernamevalid , because "it is not defined".