简体   繁体   中英

Angular2: inline template:8:0 caused by: No provider for String

I wrote the following code in Angular2:

add-item.component.ts

@Component({
  selector: 'add-item',
  template: `<form-item [title]="abcde"></form-item>`,
})

export class AddItemComponent {
  forms: Object[]

  constructor() {
    this.forms = [{title: 'a', link: 'b'}];
  }
}

form-item.component.ts

@Component({
  selector: 'form-item',
  template: `<h3>{{title}}</h3>`,
  inputs: ['title'],
  styles: [`
    h3 { 
        color: blue;      
    }
  `]
})

export class FormItemComponent{

  @Input()
  title: string;

  constructor(title: string) {
    this.title = title;
  }
}

When I run this code I get an error:

Error: Error in ./AddItemComponent class AddItemComponent - inline template:8:0 caused by: No provider for String!

I did a small research and I don't understand what is the problem. I also looked here: Angular2 EXCEPTION No provider for String

Can you please help me?

You need to remove title: string and initialization from a constructor. Angular's constructor expecting providers as arguments.

FormItemComponent class should be:

export class FormItemComponent{
  @Input() title: string = "";
}

'title' must not be initialized inside a constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM