manidos
manidos

Reputation: 3464

How to use decorators on object properties

In the folloowing code I assign template element reference to container property on MyComponent class.

class MyComponent {
  @ViewChild('container') container;
  log(){
    console.log(this.container);
  }
}

How can I create container property on an object(the code below gives me an error)

class MyComponent {
  myObject = {
     @ViewChild('container') container;
  }
  log(){
    console.log(this.myObject.container);
  }
}

Error: Propery assignment expected

Upvotes: 1

Views: 54

Answers (1)

Tim Consolazio
Tim Consolazio

Reputation: 4888

That line where you try to set up an object to equal the view container looks wonky.

Objects are created with key value pairs. But you are only providing a value.

Try this (I'm not sure why you'd want to do this, but I guess if you're going to, you'd have to do something like this):

@ViewChild('container') myContainer;
myObject = {
     container : this.myContainer
}

Upvotes: 3

Related Questions