How to change a value to a global variable in an area using a function? ("controller how")

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(){
         //I need to set this variable to true;
         this.usernamevalid = false;




         //In the View, I trigger this function
        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) {
                                //The user exist (DOES NOT WORK)
                                this.usernamevalid = true;
                            } else {
                                //The user don't exist (DOES NOT WORK)
                                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".

+4
1

this $http.get .then this .

, , .

var vm = this;

this vm. this vm , this

ngApp.controller('controller', Main);
   function Main() {
     var vm = this; //created local variable which will have this reference.
     //I need to set this variable to true;
     vm.usernamevalid = false;
     //In the View, I trigger this function
     vm.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) {
                 //The user exist (DOES NOT WORK)
                 vm.usernamevalid = true;
               } else {
                 //The user don't exist (DOES NOT WORK)
                 vm.usernamevalid = false;
               }
               break;
             default:
               console.log('Field undefined, STOP');
           }
         }, function(response) {
           alert(response + "(error)");
         });
       }
   }
 }

+4

Source: https://habr.com/ru/post/1621810/


All Articles