2
votes

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

2 Answers

2
votes

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.

2
votes

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.