Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在angularjs脚本中获取表单和输入字段的状态,而不从html中传递它们?_Javascript_Angularjs_Html - Fatal编程技术网

Javascript 如何在angularjs脚本中获取表单和输入字段的状态,而不从html中传递它们?

Javascript 如何在angularjs脚本中获取表单和输入字段的状态,而不从html中传递它们?,javascript,angularjs,html,Javascript,Angularjs,Html,我已经编写了以下代码来执行输入字段的验证。在这里,我通过一个函数将输入字段的状态从HTML传递到脚本。但是我只需要调用函数而不传递任何参数,值应该直接在脚本中获得,而不需要从HTML传递 HTML部分: <form ng-submit="LoginButton(LoginForm.$valid)" novalidate="novalidate" name="LoginForm"> <div class="row"> <div class="input-f

我已经编写了以下代码来执行输入字段的验证。在这里,我通过一个函数将输入字段的状态从HTML传递到脚本。但是我只需要调用函数而不传递任何参数,值应该直接在脚本中获得,而不需要从HTML传递

HTML部分:

<form ng-submit="LoginButton(LoginForm.$valid)" novalidate="novalidate" name="LoginForm">
  <div class="row">
    <div class="input-field col s12 center-align">
      <input id="username" type="text" ng-model="username" name="username" required>
      <label for="username">User Name</label>
      <span style="color:red;font-size:small" class="help-block" ng-show="FieldRequiredFunc(LoginForm.username.$error.required,LoginForm.username.$pristine,submitted)"
        ng-bind="FieldRequired"></span>
    </div>
  </div>

  <div class="row">
    <div class="input-field col s12 center-align">
      <input id="password" type="password" ng-model="password" name="password" required>
      <label for="password">Password</label>
      <span style="color:red; font-size:small" class="help-block" ng-show="FieldRequiredFunc(LoginForm.password.$error.required,LoginForm.password.$pristine,submitted)"
        ng-bind="FieldRequired"></span>

    </div>
  </div>

  <div class="row">
    <div class="col s12 center-align">
      <button class="custom-btn" id="Submitbtn" type="submit" name="action">Login</button>
    </div>
  </div>
</form>

我的问题是如何从html中获取脚本中的
LoginForm.username.$error.required,LoginForm.username.$pristine,…
值,而不通过
FieldRequiredFunc()
函数?

所有表单都直接绑定到
$scope
。因此,您可以通过

“$scope.LoginForm.username.$error.required,LoginForm.username.$pristine,…

//代码在这里
var-app=angular.module('myApp',[]);
app.controller('mainCtrl',['$scope',函数($scope){
$scope.LoginButton=函数(){
log(“用户名是否有效”+:“+$scope.LoginForm.username.$valid”);
};
}]);

使用ng表单的角度1.x应用程序
用户名

密码
登录
 loginModule.controller("loginViewModel", function($scope) {
   $scope.submitted = false;
   $scope.FieldRequired = "This Field is Required";
   $scope.LoginButton = function(valid) {
     $scope.submitted = true;
   }
   $scope.FieldRequiredFunc = function(cond1, cond2, submitted) {
     if (cond1 && (!cond2 || submitted))
       return true;
     else
       return false;
   }
   $scope.AllFieldRequiredFunc = function(valid, submitted) {
     if (!valid && submitted)
       return true;
     else
       return false;
   }
 });