1

I would like to ask you, how can I achieve this:

I have a component (lets name it FeatureComponent) with some box and inside this box three buttons. In another component (MainComponent) I'm using my FeatureComponent by selector tag . Now what I would like to do is to use selector tag from FeatureComponent like that:

<featurecomponent buttonOne="invisible" buttonTwo="disabled"></featurecomponent>

I've read about @Input & @Output and about directives, but I'm not sure if it's the right way.

Could someone advise me what I should use to achieve my goal?

EDIT:

Featurecomponent:

div class="group-radio-buttons">
<input type="radio"
      value="1"
      name="qaz"checked><small> buttonOne</small></div>
<input type="radio"
      value="2"
      name="qaz"><small> buttonTwo</small></div>
<input type="radio"
      value="3"
      name="qaz"><small> buttonThree</small></div>
</div>

And what I would like to achieve is to use this component in many other components but with possibility to manage this radio buttons like:

AnotherComponent:

<featurecomponent buttonOne="invisible" buttonTwo="disabled"></featurecomponent>

AnotherComponent2:

<featurecomponent buttonTwo="disabled"></featurecomponent>
2
  • 1
    It's not clear to me what you try to accomplish. If you add @Input() buttonOne:string; to your components class you can access the value using ngOnInit() { console.log(this.buttonOne); } The input value won't be available in the constructor which is executed before ngOnInit. angular.io has a lot of great tutorials Commented Feb 20, 2018 at 8:29
  • @GünterZöchbauer I've edited my post to make it more understandable. Commented Feb 20, 2018 at 8:49

1 Answer 1

1
<featurecomponent buttonOne="invisible" buttonTwo="disabled"></featurecomponent>

Inputs allow to pass values to a component

class FeatureComponent {
  @Input() buttonOne:string;
  @Input() buttonTwo:string;
  @Input() buttonThree:string;
}

You can use view bindings to use the input values in the components template

<div class="group-radio-buttons">
<input type="radio"
      [attr.disabled]="buttonOne === 'disabled' ? true : null"
      [hidden]="buttonOne === 'invisible'
      value="1"
      name="qaz"checked><small> buttonOne</small></div>
<input type="radio"
      [attr.disabled]="buttonTwo === 'disabled' ? true : null"
      [hidden]="buttonTwo === 'invisible'
      value="2"
      name="qaz"><small> buttonTwo</small></div>
<input type="radio"
      [attr.disabled]="buttonThree === 'disabled' ? true : null"
      [hidden]="buttonThree === 'invisible'
      value="3"
      name="qaz"><small> buttonThree</small></div>
</div>

Disabled is a special attribute that has an effect no matter what the value is, this is why we use the for where we pass true or null because Angular completely removes an attribute if the value is null.

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

2 Comments

Thank you so much! Working perfectly.:)
Glad to hear. .

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.