如何在Js控制器中使用我的HTML中的变量

时间:2022-10-31 18:55:51

I want to validate my form with ng-disable.

我想用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.

所以我的函数将检查用户输入的ID是否正确。如果所有输入都已填满,我还必须检查我的表格。

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.

我想带vlid。它将包含true或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?

我想检查registerForm和vlid都是假的。有没有办法从我的控制器中引用我的vlid?

3 个解决方案

#1


2  

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.

无法查看控制器的定位位置和方式使得这更加困难,但是,您可以从控制器访问范围变量。因此,有两种可能的方法可以定义控制器:使用ControllerAs:你可以在mytml中将myController作为someName。然后,您可以将someName.vlid传递给您的函数,然后将其绑定到res.data.status绑定到$ scope:在您的控制器中,您将拥有一个参数$ scope然后您可以在控制器或ng中使用$ scope.vlid你的html中的-model ='vlid',将其传递给你的函数,并以同样的方式处理它。如果您已在控制器中定义了该功能,则可以使用ng-model ='vlid'或ng-model ='someName.vlid',具体取决于控制器的定义方式。

#2


2  

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.

将变量vlid设置为控制器中的$ scope变量($ scope.vlid = vlid)。然后你可以在你的html视图中访问它。您需要更新ng-disabled表达式以包含vlid的条件。

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

#3


0  

you can refer vlid using $scope.

你可以使用$ scope来引用vlid。

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

HTML

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

#1


2  

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.

无法查看控制器的定位位置和方式使得这更加困难,但是,您可以从控制器访问范围变量。因此,有两种可能的方法可以定义控制器:使用ControllerAs:你可以在mytml中将myController作为someName。然后,您可以将someName.vlid传递给您的函数,然后将其绑定到res.data.status绑定到$ scope:在您的控制器中,您将拥有一个参数$ scope然后您可以在控制器或ng中使用$ scope.vlid你的html中的-model ='vlid',将其传递给你的函数,并以同样的方式处理它。如果您已在控制器中定义了该功能,则可以使用ng-model ='vlid'或ng-model ='someName.vlid',具体取决于控制器的定义方式。

#2


2  

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.

将变量vlid设置为控制器中的$ scope变量($ scope.vlid = vlid)。然后你可以在你的html视图中访问它。您需要更新ng-disabled表达式以包含vlid的条件。

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

#3


0  

you can refer vlid using $scope.

你可以使用$ scope来引用vlid。

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

HTML

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