Я создал HTML с обязательными полями, а затем кнопку «Отправить» с помощью ng-click=add().
Код следующий:
Enter name: <div class = "mb-3">
<label class = "form-label">Enter surname:</label>
<input type = "text" class = "form-control" placeholder = "Person surname" ng-model= "newPerson.surname" required>
</div>
<div class = "mb-3">
<label class = "form-label">Enter age:</label>
<input type = "text" class = "form-control" placeholder = "Person age" ng-model= "newPerson.age" required>
</div>
<div class = "mb-3">
<label class = "form-label">Enter occupation:</label>
<input type = "text" class = "form-control" placeholder = "Person occupation" ng-model= "newPerson.occupation" required>
</div>
<button type = "submit" class = "btn btn-primary" ng-click = "add()">Add</button>
</form>
При нажатии на кнопку добавляет человека и потом мне говорит что поля обязательны но в моем списке человек уже есть неопределенные значения.
Я также объявил в верхней части документа.
Спасибо
Извините, но что вы имеете в виду под «при отправке»? Спасибо
Спасибо, я только что узнал способ.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы также можете использовать метод проверки для проверки входных данных при отправке вместо обязательного. пример
public validateForm() {
if (newPerson.surname === null ||
newPerson.surname === undefined || newPerson.surname === " ")
{
error.surname = "Surname is required";
}
if (newPerson.age === null ||
newPerson.age === undefined || newPerson.age === " ")
{
error.age = "Age is required";
}
if (newPerson.occupation === null ||
newPerson.occupation === undefined || newPerson.occupation === " ")
{
error.occupation = "Occupation is required";
}
if (newPerson.surname !== null ||
newPerson.surname !== undefined || newPerson.surname !== " " ||
newPerson.age !== null ||
newPerson.age !== undefined || newPerson.age !== " " ||
newPerson.occupation !== null ||
newPerson.occupation !== undefined || newPerson.occupation !== "
)
{
this.add();
}
} <div class = "mb-3">
<label class = "form-label">Enter surname:</label>
<input type = "text" class = "form-control" placeholder = "Person surname" ng-model= "newPerson.surname" required>
</div>
<span *ngIf = "error.surname"> {{error.surname}}</span>
<div class = "mb-3">
<label class = "form-label">Enter age:</label>
<input type = "text" class = "form-control" placeholder = "Person age" ng-model= "newPerson.age" required>
</div>
<span *ngIf = "error.age"> {{error.age}}</span>
<div class = "mb-3">
<label class = "form-label">Enter occupation:</label>
<input type = "text" class = "form-control" placeholder = "Person occupation" ng-model= "newPerson.occupation" required>
</div>
<span *ngIf = "error.occupation"> {{error.occupation}}</span>
<button type = "submit" class = "btn btn-primary" ng-click = "validateForm()">Add</button>
</form>
Вызывайте
add()при отправке, а не при нажатии кнопки.