source

Angularjs에서 제출한 후 폼 재설정

factcode 2023. 2. 9. 22:49
반응형

Angularjs에서 제출한 후 폼 재설정

안녕하세요, 저는 버튼 클릭 시 업데이트를 하는 폼을 가지고 있습니다.

 $scope.action = "Update";
  var id = $routeParams.editId;
  scope.item = updateRecord.get({ id: id });

항목이 업데이트되면 양식 필드에 입력된 정보는 제거되지 않습니다.angularjs에서 위 코드에 추가할 수 있는 것이 무엇인지 궁금해서 업로드 후 폼으로 클리어합니다.감사해요.

다음 방법으로 양식을 재설정할 수 있습니다.$scope.formName.$setPristine();그러나 모델 객체를 입력에 바인딩하는 경우 해당 항목도 삭제해야 합니다.

$scope.currentRecord={};

편집

Toodo N-Mike가 지적했듯이, 잊지 말고

$scope.formName.$setUntouched()

$touchedflag는 각도 1.3에서 도입되었습니다.

송신 기능의 본문 하단에서, 다음의 코드를 실행합니다.

// Reset the form model.
vm.project = {};
// Set back to pristine.
vm.form.$setPristine();
// Since Angular 1.3, set back to untouched state.
vm.form.$setUntouched();

"vm.form"이 폼 이름입니다.

상세한 것에 대하여는, 다음의 메뉴얼 페이지를 참조해 주세요.https://docs.angularjs.org/api/ng/type/form.FormController

이건 나한테 효과가 있었어.

viewModel.data = {};
$scope.formName.$setUntouched();
$scope.formName.$setPristine();

1) 폼 필드의 값을 삭제하고 리셋하려면 $setPristin()을 사용합니다.

$scope.formName.$setPristine();

2) 다음으로 폼도 Unattached State로 설정하려면 $setUntouched()를 사용합니다.

(폼 필드에 필수 필드가 있고 ng-messages를 사용하는 경우 아래 함수를 사용하지 않으면 해당 필드에 오류가 표시됩니다.)

$scope.formName.$setUntouched();

질문은 모르겠지만 HTML 컴포넌트에서 폼을 정리할 수 있습니다.

함수: ngSubmit(), 데이터를 전송합니다.taskName은 필드의 이름이며 taskBody도 마찬가지입니다.

<form (ngSubmit)="onSubmit(taskName.value, taskBody.value); taskName.value=''; taskBody.value=''" #taskForm="ngForm">

언급URL : https://stackoverflow.com/questions/21571370/resetting-form-after-submit-in-angularjs

반응형