Slemgrim
Slemgrim

Reputation: 957

Angular dart component init

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?

Upvotes: 2

Views: 337

Answers (2)

Mike Mitterer
Mike Mitterer

Reputation: 7180

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.

Upvotes: 2

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657288

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.

Upvotes: 2

Related Questions