简体   繁体   English

无法在Angular 4中绑定属性

[英]Can't Bind Property in Angular 4

Why is there a problem in binding a property on the same component? 为什么在同一组件上绑定属性会有问题? I already added Input() but still doesn't work. 我已经添加了Input(),但仍然无法正常工作。 Do i need to put Input() even though it is on the same component when binding? 绑定时我是否需要将Input()放在同一组件上?

//output.component.ts
import { Component, OnInit} from '@angular/core';
import { DataService } from '../data.service';
@Component({
  selector: 'app-output',
  templateUrl: './output.component.html',
  styleUrls: ['./output.component.css']
})
export class OutputComponent implements OnInit {
data: {name: string};
datas = [];

constructor(private dataService: DataService) { }

ngOnInit(){
   this.datas = this.dataService.datas;
}
}



//output.component.html
<p *ngFor="let data of datas"></p>
<p>{{data.name}}</p>


//data.service.ts
export class DataService {
  datas= [];

  addData(name: string){
     return this.datas.push({name: name});
  } 
}

For same component @input API is not required. 对于相同的组件,不需要@input API It is used when you want to pass the data from Parentcomponent to a child component. 当您要将数据从Parentcomponent传递到子组件时使用。

//output.component.html
<p *ngFor="let data of dataService.datas" >  // removed [data]="data" and added dataService.datas
   <p>{{data?.name}}</p>
</p>                                         //changed the position of </p>


export class OutputComponent implements OnInit {
   constructor(private dataService: DataService) {}
}

export class DataService {
   datas= [];

   addData(name: string){
      return this.datas.push({name: name});   //return keyword was missing
   } 
}

Just for your reference 仅供参考

DEMO: https://plnkr.co/edit/XlJM2LHFwlAYpQe2ancM?p=preview 演示: https : //plnkr.co/edit/XlJM2LHFwlAYpQe2ancM? p =preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 7:无法绑定到“元素”,因为它不是 - Angular 7 : Can't bind to 'element' since it isn't a known property of 无法绑定到“”,因为它不是“ angular 2”的已知属性 - can't bind to '' since it isn't a known property of '' angular 2 Angular 8 - 无法使用指令 - 无法绑定,因为它不是已知属性 - Angular 8 - Cannot use directives - can't bind since it is not a known property 带有 Angular 的 NG2 图表:无法绑定到“颜色”,因为它不是 Angular 中“画布”的已知属性 13 - NG2 Chart with Angular: Can't bind to 'colors' since it isn't a known property of 'canvas' in Angular 13 无法绑定到“属性”,因为它不是“选择器”的已知属性 - Can't bind to 'property' as it is not a known property of 'selector' Angular 4无法绑定到&#39;formGroup&#39;,因为它不是&#39;form&#39;的已知属性 - Angular 4 Can't bind to 'formGroup' since it isn't a known property of 'form' 无法绑定到((ngModel),因为在角度单位测试用例中它不是&#39;input&#39;的已知属性 - Can't bind to '(ngModel' since it isn't a known property of 'input' in angular unit test case 无法绑定到“ formGroup”,因为它不是“ form”的已知属性。 (“角7 - Can't bind to 'formGroup' since it isn't a known property of 'form'. (" in angular 7 无法绑定到“ ngModule”,因为它不是“ input”的已知属性。 在angular 2 asp.net核心中 - Can't bind to 'ngModule' since it isn't a known property of 'input'. in angular 2 asp.net core 无法绑定到“paint”,因为它不是 Angular 9 中带有 mapbox 的“mgl-layer”的已知属性 - Can't bind to 'paint' since it isn't a known property of 'mgl-layer' with mapbox in Angular 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM