Я пытаюсь получить / установить значение для ввода Html, используя [(ngModel)] Evrything прекрасно работает с моим 2d уровнем Json, или даже с 3D уровнем Json, ЕСЛИ мой объект уже существует в моем json.Но, когда мой 3d-объект равен NULL, я больше не могу использовать двойную привязку.поэтому я отделяю свой NgModel ([ngModel] для чтения и (ngModelChange) для установки) и это моя проблема, я не могу установить объект undefined.потому что структура моего 3d-объекта не существует.
Я пытался создать модель с моими атрибутами к югу от модели, но даже с моей моделью он не может установить мои атрибуты.
- Это мой ответ от моего WebApi:
"Data": {
"Entreprise": {
"PostalAddressSameAsCorrespondence": false,
"ID_Entreprise": 3374,
"Prefixe": null,
"Suffixe": null,
"ID_EntrepriseParent": null,
"ID_EntrepriseParentIcriq": null,
"TypeEntrepriseLookup": "ENSE",
"EntrepriseSaisonniere": "N",
...
"TelephonePrincipal": {
"ID_Telephone": 716,
"ID_Entreprise": 374,
"ID_Contact": null,
"TypeTelephoneLookup": "PRI",
"NumeroTelephone": "999-328-4200",
"Poste": null,
"DateCreation": "2017-03-29T15:27:04",
"CodeUsagerCreation": "ASDQQ",
"DateModification": null,
"CodeUsagerModification": null
},
"TelephoneSecondaire": null
}
- Вот как я читаю и устанавливаю значение на 2d уровне
<div class="md-form">
<input type="text" id="nomLegalFrancais" class="form-control" mdbInput mdbValidate
[(ngModel)]="entrepriseModel.NomLegalFrancais" formControlName="nomLegalFrancais">
<label for="nomLegalFrancais">Nom légal français *</label>
<mdb-error *ngIf="nomLegalFrancais.invalid && (nomLegalFrancais.dirty || nomLegalFrancais.touched)">
Entrée invalide</mdb-error>
<mdb-success *ngIf="nomLegalFrancais.valid && (nomLegalFrancais.dirty || nomLegalFrancais.touched)">
Entrée valide</mdb-success>
</div>
- вот как я пытаюсь прочитать и установить значение в 3d-уровне (например, TelephoneSecondaire)
<!-- Telephone sans frais -->
<div class="col-md-6 col-sm-6">
<div class="md-form form-group">
<input id="telephoneSansFrais" mdbInput mdbValidate type="text" class="form-control"
[ngModel]="entrepriseModel?.TelephoneSansFrais?.NumeroTelephone"
(ngModelChange)="entrepriseModel.TelephoneSansFrais?.NumeroTelephone = $event"
[textMask]="{mask: phoneSansFraisMask}" formControlName="telephoneSansFrais"> <!-- Setter (ngModelChange) NOT WORKING -->
<label for="telephoneSansFrais">Téléphone sans frais</label>
</div>
</div>
export class Entreprise {
public ID_Entreprise: number;
public Prefixe: string;
public Suffixe: string;
public ID_EntrepriseParent: number;
public ID_EntrepriseParentIcriq: number;
public NomLegalFrancais: string;
...
public TelephonePrincipal: Telephone;
public TelephoneSansFrais: Telephone;
deserialize(input: any): this {
Object.assign(this, input.Data.Entreprise);
return this;
}
}
export class Telephone implements {
public ID_Telephone: number;
public ID_Entreprise: number;
public ID_Contact: number;
public TypeTelephoneLookup: string;
public NumeroTelephone: string;
public Poste: string;
public DateCreation: string;
public CodeUsagerCreation: string;
public DateModification: string;
public CodeUsagerModification: string;
}
Я ожидаю получить JSON, как это:
Использование динамических объектов:
"Data": {
"Entreprise": {
"PostalAddressSameAsCorrespondence": false,
"ID_Entreprise": 3374,
"Prefixe": null,
"Suffixe": null,
"ID_EntrepriseParent": null,
"ID_EntrepriseParentIcriq": null,
"TypeEntrepriseLookup": "ENSE",
"EntrepriseSaisonniere": "N",
...
"TelephonePrincipal": {
"ID_Telephone": 716,
"ID_Entreprise": 374,
"ID_Contact": null,
"TypeTelephoneLookup": "PRI",
"NumeroTelephone": "999-328-4200",
"Poste": null,
"DateCreation": "2017-03-29T15:27:04",
"CodeUsagerCreation": "ASDQQ",
"DateModification": null,
"CodeUsagerModification": null
},
"TelephoneSecondaire": {
"NumeroTelephone": "123-465-999", <!-- my input id="telephoneSansFrais" value -->
}
}
Или это, если я использую мой класс "Телефон":
"Data": {
"Entreprise": {
"PostalAddressSameAsCorrespondence": false,
"ID_Entreprise": 3374,
"Prefixe": null,
"Suffixe": null,
"ID_EntrepriseParent": null,
"ID_EntrepriseParentIcriq": null,
"TypeEntrepriseLookup": "ENSE",
"EntrepriseSaisonniere": "N",
...
"TelephonePrincipal": {
"ID_Telephone": 716,
"ID_Entreprise": 374,
"ID_Contact": null,
"TypeTelephoneLookup": "PRI",
"NumeroTelephone": "999-328-4200",
"Poste": null,
"DateCreation": "2017-03-29T15:27:04",
"CodeUsagerCreation": "ASDQQ",
"DateModification": null,
"CodeUsagerModification": null
},
"TelephoneSecondaire": {
"ID_Telephone": null,
"ID_Entreprise": null,
"ID_Contact": null,
"TypeTelephoneLookup": null,
"NumeroTelephone": "123-465-999", <!-- my input id="telephoneSansFrais" value -->
"Poste": null,
"DateCreation": null,
"CodeUsagerCreation": null,
"DateModification": null,
"CodeUsagerModification": null
},
}