简体   繁体   English

更改子组件的值时,Angular 父组件属性不会更新

[英]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.

相关问题 Angular Observable 从父组件到子组件获取更新的更改对象 - Angular Observable Get updated changed object from a parent to child component 从子组件以角度形式获取父代的属性 - Get parent's property from child component in angular form 更改子组件中属性的值不起作用 - Changing the value of a property in child component does not work angular 父组件属性更新不运行子组件中的方法 - angular parent component property update does not run methods in child component 角度4:如何在不由父级执行任何额外工作的情况下,通过子级组件中的更改值来更新父级组件的值 - Angular 4: How can I get the Parent Component's value to be updated by a changed value in a child Component without the Parent doing any extra work Angular @从父组件更改时输入值不变 - Angular @Input value not changing when changed from Parent Component 如何通过更改Angular4中子组件中的值来更新父组件中数组的长度? - How can I update length of array in parent component from changing value in child component in Angular4? 兄弟组件未从子组件获取更新的参数值 - Angular - Sibling component is not getting updated parameter value from child component - Angular 在Angular的“子组件”中单击按钮时,如何为父组件属性设置值 - How to set value to parent component property when a button is clicked in Child component in Angular 从Angular 2中的子组件更新父组件属性 - Update parent component property from child component in Angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM