我必须在Angular 5中传递来自子组件和父组件的数据,我使此类学习了一些教程,并尝试在plunker(或stackblitz)上复制它们,问题是我的父组件没有。当我调用一些填充(或步枪)儿童表单的儿童函数时,用儿童数据进行更新,当我在这些表单中手动编写内容时,父亲组件会正确更新,这是儿童类:
typedef uint32 Fls_AddressType;
在HTML文件中,我绑定了这样的字段(公司字段示例):
import { Component, Input } from "@angular/core";
import * as components from '../../component';
import * as viewModels from "viewmodel";
import * as companyService from "services/component/company.service";
import * as userService from "services/component/user.service";
@Component({
selector: 'user-company',
templateUrl: require('./user-company.component.html'),
providers: [userService.UserService, companyService.CompanyService]
})
export class UserCompanyComponent {
/** Object with form properties sended to father component*/
@Input() CompanySelezionata: viewModels.CompanyViewModel;
@Input() UtenteSelezionato: viewModels.UserViewModel;
private _pIVA: string;
private _userEmail: string;
/** boolean variables for lock-unlock children's forms
private _companyUnlock: boolean;
private _userUnlock: boolean;
/** boolean variables needed for showing some messages on DOM*/
private _hideCompanyMessage: boolean;
private _hideUserMessage: boolean;
/** services for children component */
constructor(private _userService: userService.UserService, private
_companyService: companyService.CompanyService) {
this.initUser();
this.initCompany();
this._companyUnlock = false;
this._userUnlock = false;
this._hideCompanyMessage = true;
this._hideUserMessage = true;
}
/** init user object */
public initUser() {
this.UtenteSelezionato = new viewModels.UserViewModel();
}
/** init company object */
public initCompany() {
this.CompanySelezionata = new viewModels.CompanyViewModel();
}
// Call API for searching company(children updated corrctly, father not
updated)
public cercaCompany(company: string) {
if (company == undefined || company == "") {
this.initCompany();
return;
}
this._companyService.GetByVatCode(company)
.subscribe((result) => {
if (result == null) {
this._companyUnlock = true;
this._hideCompanyMessage = false;
this.initCompany();
this.CompanySelezionata.VatCode = this._pIVA;
return;
}
this.CompanySelezionata = result;
});
}
// Call API for searching user (children updated corrctly, father not
updated)
public cercaUtente(userEmail: string) {
if (userEmail == undefined || userEmail == "") {
return this.initUser();
}
this._userService.GetByEmail(userEmail)
.subscribe((result) => {
if (result == null) {
this._userUnlock = true;
this._hideUserMessage = false;
this.initUser();
this.UtenteSelezionato.EMail = this._userEmail;
return;
}
this.UtenteSelezionato = result;
});
}
// CLEAN CHILDREN'S FORM, father gets no update when calls it
public cleanAll() {
this.cleanUser();
this.cleanCompany();
}
// Clean user's form, called by children it's ok (object updated only in
children)
public cleanUser() {
this._userEmail = '';
this._hideUserMessage = true;
this._userUnlock = false;
this.initUser();
}
// Clean company's form, called by children it's ok (object updated only
in children)
public cleanCompany() {
this._pIVA = '';
this._hideCompanyMessage = true;
this._companyUnlock = false;
this.initCompany();
}
}
这是父亲的课:
<label class="k-form-field">
<span>Nome</span>
<span class="required">
<input type="text" class="k-textbox" name="nomeLegRap"
[disabled]="this._companyUnlock == false"
[(ngModel)]="CompanySelezionata.InformazioniAggiuntive.
LegaleRappresentante.Nome" />
</span>
</label>
最后这是父HTML:
import { Component, ViewChild, Input } from "@angular/core";
import * as viewmodels from "viewmodel";
import { UserCompanyComponent } from "../../../shared-component";
@Component({
selector: 'app-tester-manager',
templateUrl: require('./tester-manager.component.html'),
})
export class TesterManagerComponent {
currentCompany: viewmodels.CompanyViewModel = new
viewmodels.CompanyViewModel();
currentUser: viewmodels.UserViewModel = new viewmodels.UserViewModel();
// calls children's methods
@ViewChild(UserCompanyComponent) childUserCompany: UserCompanyComponent;
constructor() {
}
// Call method cleaning children's form
public cleanAll() {
this.childUserCompany.cleanAll();
}
}
所以问题是,如果我先在儿童表格中手动编写(或删除),父亲就只会更新,