[英]Angular Parent Component property does not get updated when changing value from Child Component
Below I don't understand why I'm getting the compilation error Cannot use variable 'ft' as the left-hand side of an assignment expression. Template variables are read-only.
下面我不明白为什么我会收到编译错误
Cannot use variable 'ft' as the left-hand side of an assignment expression. Template variables are read-only.
Cannot use variable 'ft' as the left-hand side of an assignment expression. Template variables are read-only.
If I remove the banana-syntax and leave it as [fileType], then I have no errors, but when I change the value from the Select control in the child component, the item in the parent collection fileTypes does not get updated.如果我删除香蕉语法并将其保留为 [fileType],那么我没有错误,但是当我更改子组件中 Select 控件的值时,父集合 fileTypes 中的项目不会更新。 How should I do it so the parent collection gets updated ?
我应该怎么做才能更新父集合?
parent component父组件
<app-file-type-list-item class="form-row align-items-end" *ngFor="let ft of fileTypes" [(fileType)]="ft"></app-file-type-list-item>
child component子组件
<select name="fileType01" id="fileType01" [(ngModel)]="fileType" (change)="onFileTypeChange()">
<option *ngFor="let fileType of fileTypes" [ngValue]="fileType">
{{fileType.name}}</option>
</select>
export class FileTypeListItemComponent {
@Input() public fileType: FileType;
@Output() fileTypeChange = new EventEmitter();
public fileTypes;
constructor(private _entityManagerService: POPEntityManagerService) {
this.fileTypes = // filled from datasource
onFileTypeChange(): void {
this.fileTypeChange.emit(this.fileType);
}
}
Can you try using this syntax:您可以尝试使用以下语法:
<app-file-type-list-item class="form-row align-items-end" *ngFor="let ft of fileTypes; let i = index" [(fileType)]="fileTypes[i]"></app-file-type-list-item>
In case if it doesnt work plesase provide details for Angular compiler options in case these options are not enabled如果它不起作用,请提供 Angular 编译器选项的详细信息,以防这些选项未启用
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.