2

Is there a way to wait for the component to be initialized?

@Component(
  selector: "my-component",
  templateUrl: 'component/my.html',
  useShadowDom: false,
  publishAs: "ctrl"
)
class MyComponent {

  @NgAttr('foo')
    String foo;
  }

  @NgAttr('bar')
    String bar;
  }

  MyComponent(){
    print(foo);
    print(bar);
  }
}

Usage:

<my-component foo="bar" bar="baz"></my-component>

When i use the component like this, the constructor prints: null, null

I could write a setter for foo and bar and check on every set if they are both set. But if no value is provided.. my init is never fired.

Do i have to implement a interface which provides a init method or something?

2 Answers 2

2

It's the same as shown here Connecting 2 controllers and have access to the first controllers propertie in the second controller

Implement the AttachAware interface. The values of the instance can't be set before the element is constructed therefore there is no chance to have the fields set from the outside when the constructor is executed.

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

Comments

2

As Günter said implement either AttacheAware or!! ShadowRootAware. onShadowRoot comes after onAttachAware. The Param it gives you (ShadowRoot) is your custom element. The name is misleading - it also works if useShadowDom is false.

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.