如何在控制器中验证我的角度形式?我需要检查表格并取决于有效性显示文本块
我使用了来自官方 angularjs 网站的表单示例。
有人可以解释一下为什么这段代码console.log($scope.myForm);
输出未定义,我怎样才能得到表格?
angular.module('formExample', [])
.controller('FormController', ['$scope', function($scope) {
$scope.userType = 'guest';
if(myForm.$valid){ //I tried to validate form
console.log('Form is valid ' + $scope.myForm.$valid);
}else{
console.log('form is not valid ' + $scope.myForm.$valid);
}
}]);
.my-form {
transition:all linear 0.5s;
background: transparent;
}
.my-form.ng-invalid {
background: red;
}
</s
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-example59-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular-animate.js"></script>
<script type="text/javascript">
angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
</script>
</head>
<body ng-app="formExample">
<form name="myForm" ng-controller="FormController" class="my-form">
userType: <input name="input" ng-model="userType" required>
<span class="error" ng-show="myForm.input.$error.required">Required!</span><br>
<code>userType = {{userType}}</code><br>
<code>myForm.input.$valid = {{myForm.input.$valid}}</code><br>
<code>myForm.input.$error = {{myForm.input.$error}}</code><br>
<code>myForm.$valid = {{myForm.$valid}}</code><br>
<code>myForm.$error.required = {{!!myForm.$error.required}}</code><br>
</form>
</body>
</html>