0

I'm new to angular2.in my react project, my component looks like this:

export const DefaultButton = (props: Props) => {
  return <button {...props}>{props.children}</button>
}

<DefaultButton>delete</DefaultButton>

ng2:

@Component({
    selector: "button-default",
    template`<button>{{text}}</button>`,
    styleUrls: ["./default.css"]
})
export class ButtonDefaultComponent {
    @Input() private text: string
}

can i use ng2 like this?

<button-default>delete</button-default>
2

3 Answers 3

1

The other two answers are close, but I think what you want is in the comment by @yurzui. You don't need an @Input decorated value, if all you're trying to do is pass down the word "delete" like in React. You can just use <ng-content>.

@Component({
  selector: "button-default",
  template: '
  <button>
    <ng-content></ng-content>
  </button>
  ',
  styleUrls: ["./default.css"]
})
export class ButtonDefaultComponent { }

and

<button-default> Delete </button-default>

Sign up to request clarification or add additional context in comments.

Comments

0

If i understood you correctly, you want to have "delete" string inside your component as part of text input variable.

Your ts code is good, just change implementation of component in html:

<button-default [delete]="'delete'"></button-default>

Comments

0

Component

@Component({
selector: "button-default",
template`<button>{{text}}</button>`,
styleUrls: ["./default.css"]
})

export class ButtonDefaultComponent {
   @Input("delete") private text: string
} 

index

<button-default delete="delete"></button-default>

that is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.