반응형
각도 JS동봉하지 않은 검증
Angular에서 격리된 단일 시스템을 검증할 수 있습니까?<input>
양식이 검증되는 것과 유사한 방법으로요?저는 이렇게 생각하고 있습니다.
<div class="form-group">
<input name="myInput" type="text" class="form-control" ng-model="bindTo" ng-maxlength="5">
<span class="error" ng-show="myInput.$error.maxlength">Too long!</span>
</div>
위의 예는 효과가 없습니다.에 봉입하다<form>
및 교환ng-show
와 함께ng-show="myForm.myInput.$error.maxlength"
도와준다.
를 사용하지 않고 이 작업을 수행할 수 있습니까?<form>
?
ng-form angular 디렉티브(여기서 docs 참조)를 사용하여 html 형식 외에서도 모든 것을 그룹화할 수 있습니다.그런 다음 angular FormController를 활용할 수 있습니다.
<div class="form-group" ng-form name="myForm">
<input name="myInput" type="text" class="form-control" ng-model="bindTo" ng-maxlength="5">
<span class="error" ng-show="myForm.myInput.$error.maxlength">Too long!</span>
</div>
Silvio Lucas의 답변을 바탕으로 반복하여 양식 이름과 유효한 상태를 삽입할 수 있어야 하는 경우:
<div
name="{{propertyName}}"
ng-form=""
class="property-edit-view"
ng-class="{
'has-error': {{propertyName}}.editBox.$invalid,
'has-success':
{{propertyName}}.editBox.$valid &&
{{propertyName}}.editBox.$dirty &&
propertyValue.length !== 0
}"
ng-switch="schema.type">
<input
name="editBox"
ng-switch-when="int"
type="number"
ng-model="propertyValue"
ng-pattern="/^[0-9]+$/"
class="form-control">
<input
name="editBox"
ng-switch-default=""
type="text"
ng-model="propertyValue"
class="form-control">
<span class="property-type" ng-bind="schema.type"></span>
</div>
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"> </script>
</head>
<body ng-controller="MainCtrl">
<div class="help-block error" ng-show="test.field.$error.required">Required</div>
<div class="help-block error" ng-show="test.firstName.$error.required">Name Required</div>
<p>Hello {{name}}!</p>
<div ng-form="test" id="test">
<input type="text" name="firstName" ng-model="firstName" required> First name <br/>
<input id="field" name="field" required ng-model="field2" type="text"/>
</div>
</body>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.field = "name";
$scope.firstName = "FirstName";
$scope.execute = function() {
alert('Executed!');
}
});
</script>
언급URL : https://stackoverflow.com/questions/22098584/angularjs-input-validation-with-no-enclosing-form
반응형
'programing' 카테고리의 다른 글
AJAX 콜을 처리하는 웹 워커 - 최적화 오버킬? (0) | 2023.02.22 |
---|---|
Django Rest Framework를 사용하여 파일을 업로드하고 JSON payload를 전송하려면 어떻게 해야 합니까? (0) | 2023.02.22 |
AngularJS : $scope를 호출할 때 이미 진행 중인 오류 $digest를 방지합니다.$syslog() (0) | 2023.02.22 |
SQL Server에서의 지연 가능한 제약 조건 (0) | 2023.02.22 |
AngularJS App: .js 파일을 index.html에 포함하는 방법 (0) | 2023.02.22 |