3

I have created a custom element and placed on a page like this:

<my-custom-element [value]="100"></my-custom-element>

In the component definition, I have this:

@Input() value: number = 50;

At run-time, the value is always 50. I expect it to be 100. If I remove the default, value is undefined. What am I missing?

Thanks!!

8
  • This code seems correct, it is difficult to know what's happening without the rest of it. Have you tried to restart npm? Commented May 31, 2018 at 15:19
  • After restart the element exhibits the same behavior. What else would you like to see in the code? Commented May 31, 2018 at 15:27
  • I removed the brackets ... um ... it is working. I could have sworn I tried that already. Now, why do all of the examples I have ever seen have the brackets? Confused. Commented May 31, 2018 at 15:48
  • i'm trying to reproduce your problem but i can't. are you sure you are printing your value and you are not looking at the beginning automated logging that prints initial value? Commented May 31, 2018 at 15:51
  • I threw the value into a console.log within the ngOnInit method. Once i removed the square brackets, things started working. I probably did something else (because that does not make sense to me), however, I am not certain what I may have done other than that to correct the problem. It would still be good to know whether my syntax is valid or not. Commented May 31, 2018 at 16:42

2 Answers 2

0

In NG Elements you may not find in your OnIt but in OnChanges.

Please add below line and check it is defined.

public ngOnChanges(): void {
    console.log('on changes value: ', this.value);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can set data using HTML attributes and to change/update data in Angular Elements you have to use vanilla js, query selector and assign data like below

custom element tag with initial value = 0

<my-custom-element value="0" ></my-custom-element>

select custom element through the query selector and assign value.

var customElement = document.querySelector('my-custom-element');
customElement.value = 100;

6 Comments

This is totally wrong. You can pass arguments the HTML attributes way in angular elements
@KavindaJayakody, if you pass in using ElementRef or HtmlElement or whatever, then the attributes have to be added/modified in ngAfterViewInit of the component. If your element needs that input on ngOnInit, then this won't work.
@KeyvanSadralodabai yeah right. But what I mentioned was, we don't actually need to do it the way this answer suggests, we can actually use html attributes for the problem. The way it should be
Kavinda Jayakody and @KeyvanSadralodabai this scenario is for changing data on runtime.
@KavindaJayakody, I think html attributes don't work with angular elements the same way they do with regular DOM elements. I haven't been successful in wrapping attributes in brackets ([ and ]) for angular elements. The input property attributes of angular elements have to be literal values, or if you need dynamic variables to use as values, then you need to inject the Renderer2, get an ElementRef and use the Renderer2 to add attributes/events, etc. Please let me know if you need further details.
|

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.