programing

각도 JS각도 JS동봉하지 않은 검증동봉하지 않은 검증

minimums 2023. 2. 22. 21:39
반응형

각도 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

반응형