具有@input属性的Angular 7 2向绑定

Oma*_*uba 2 typescript angular angular7

我有一个子组件,该子组件使用@Input指令从父组件获取属性值。问题在于,双向数据绑定似乎不适用于此输入属性。知道是什么原因吗?

子组件类

@Component({
  selector: 'app-edit-property',
  templateUrl: './edit-property.component.html',
  styleUrls: ['./edit-property.component.css']
})
export class EditPropertyComponent implements OnInit {

  @Input() property: any;

  constructor(
    private propertiesService: PropertiesService
  ) { }

  ngOnInit() {
  }
Run Code Online (Sandbox Code Playgroud)

}

模板

<input type="text" class="form-control" required name="title" [(ngModel)]="property.title" #title="ngModel">
Run Code Online (Sandbox Code Playgroud)

父组件

<app-edit-property [property]='property'></app-edit-property>
Run Code Online (Sandbox Code Playgroud)

JFP*_*ard 5

对于两种绑定方式,您必须实现一个@Output具有与Change属性相同名称的后缀,如下所示:

@Input() counter;
@Output() counterChange = new EventEmitter();
Run Code Online (Sandbox Code Playgroud)

在html中,您添加 [(counter)]="someValue"

然后您发出如下新值:

this.counterChange.emit(this.counterValue);
Run Code Online (Sandbox Code Playgroud)