0

I've a component test

test.component.ts

public getParameters() {
    return 'test.parameter';
}

test.component.html

<other-comp attr.data-target-panels="{{getParameters()}}">
</other-comp>

On this other component I've this constructor:

export class OtherComponent {

    constructor(@Attribute('data-target-panels') public targetPanels: string
    ) {

    }
}

How can i build this other-comp and binding the parameters value?

I try using

<other-comp attr.data-target-panels="{{getParameters()}}">
</other-comp>

and

<other-comp [attr.data-target-panels]="{{getParameters()}}">
</other-comp>

but it doesn't work.

2
  • Why don't you use @Input ()? Commented Oct 29, 2019 at 9:05
  • I'm using an existing component so it's not that easy to change this logic Commented Oct 29, 2019 at 9:20

1 Answer 1

1

If you are using some component in another component and binding value attribute binding with method, then method should be in the component where you are using component for example -

Component A -

<other-comp [datatargetpanels]="{{getParameters()}}">
</other-comp>

export class AComponent {

    constructor() {
    }
    public getParameters() {
       return 'Some data';
    } 
}

and In otherComponent you need to use @Input to get value like this -

export class otherComponent {
   @Input('datatargetpanels) 'datatargetpanels': string;
}
Sign up to request clarification or add additional context in comments.

3 Comments

So, I need to use input here. But what's the "real" use of @Attributes? Use it when we've some static data?
@JMarques yes, Sorry but I am not fully sure about usage of @attribute, will update here once I study about it.
@attribute is for constant string literals only. You can't bind an object to it (normally because these aren't available when the constructor is running). You have to use @Input() and pick up the input in the ngOnInit() hook.

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.