Pickles
Pickles

Reputation: 243

How do I use a variable in Js controller to my HTML

I want to validate my form with ng-disable.

So my function will check if the ID the user entered is correct.
Also I will have to check my form if all the inputs are filled up.

This is my Function:

vm.validateid = function(){
    console.log('here');
    var objvar = {
        'id': vm.data.referalid,
        'referral2': true,
    };

    $http.post(ConfigCnst.restUrl,objvar).then(function(res) {
        if (res.data.status !== true) {   

             alert('Invalid ID');
             vm.data.referalName = 'Invalid ID';
             vlid = res.data.status;
             console.log(vlid);

        } else {

            alert('ID Validated');
            vm.data.referalName = JSON.stringify(res.data.data.name);
            vlid = res.data.status;
            console.log(vlid);
        }

    });
}

I want to take vlid. It will either contain true or false.

This is my ng-disable:

 <button ng-disabled="registerForm.$invalid" 
         ng-click="register.submit()"  
         class="button button-block button-positive">Confirm</button>

I want to check both registerForm and vlid as both false.
Is there a way to reference my vlid from my controller?

Upvotes: 0

Views: 235

Answers (3)

Roland Heath
Roland Heath

Reputation: 334

Not being able to see where/how your controller is defined makes this a bit more difficult, but yes, you can access scope variables from a controller.
So, there are two likely ways your controller might be defined:

Using ControllerAs:
You would have myController as someName in your html.
You can then pass someName.vlid to your function, and then bind this to res.data.status

Binding to $scope:
In your controller, you would have a parameter $scope
You can then use $scope.vlid in your controller or ng-model='vlid' in your html, pass that to your function, and treat it in the same way.

If you've defined that function in your controller, you can use ng-model='vlid' or ng-model='someName.vlid' depending on the way your controller is defined.

Upvotes: 2

Shubham Nigam
Shubham Nigam

Reputation: 3944

you can refer vlid using $scope.

vm.validateid = function() {
  console.log('here');
  var objvar = {
    'id': vm.data.referalid,
    'referral2': true,
  };
  $http.post(ConfigCnst.restUrl, objvar).then(function(res) {
    if (res.data.status !== true) {

      alert('Invalid ID');
      vm.data.referalName = 'Invalid ID';
      vlid = res.data.status;
      $scope.vlid_status = res.data.staus;
      console.log(vlid);
    } else {

      alert('ID Validated');
      vm.data.referalName = JSON.stringify(res.data.data.name);
      vlid = res.data.status;
      $scope.vlid_status = res.data.staus;

      console.log(vlid);
    }

  });
}

HTML

<button ng-disabled="registerForm.$invalid && vlid_status === false" ng-click="register.submit()"  class="button button-block button-positive">Confirm </button>

Upvotes: 0

Navaneet
Navaneet

Reputation: 1387

Set the variable vlid to a $scope variable in your controller($scope.vlid=vlid). Then you can access it in your html view. You need to update ng-disabled expression to include condition for vlid.

<button ng-disabled="registerForm.$invalid && !vlid" ng-click="register.submit()"  class="button button-block button-positive">Confirm </button>

Upvotes: 2

Related Questions