最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass variables in an Angular selector - Stack Overflow

programmeradmin1浏览0评论

I have an angular charComponent that creates pie, and line and other graphs which I want to embed in various places in my app.

I use the selector:

<chart></chart>

To expose a chart.

What I want to do is embed a variable into the code so I can tell the ponent what chart I want returning.

So something like this:

<chart variable="pie-chart"></chart>

Is something like this possible or is there a better way to do it?

I have an angular charComponent that creates pie, and line and other graphs which I want to embed in various places in my app.

I use the selector:

<chart></chart>

To expose a chart.

What I want to do is embed a variable into the code so I can tell the ponent what chart I want returning.

So something like this:

<chart variable="pie-chart"></chart>

Is something like this possible or is there a better way to do it?

Share Improve this question edited Sep 20, 2017 at 9:20 Maxim Shoustin 77.9k29 gold badges210 silver badges228 bronze badges asked Sep 20, 2017 at 8:59 HappyCoderHappyCoder 6,1557 gold badges49 silver badges77 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

It is called property binding and is one of the core concepts you should/will be using with Angular.

  1. Define the variable, which will be passed to your ChartComponent as

    export class ChartComponent {
    
      @Input() public varName: string; // this is typed as string, but you can use any type you want
    
      constructor() {}
    }
    
  2. Now you can use

<chart [varName]="varValue"></chart>

OR

<chart varName="varValue"></chart>

to pass the variable value to the ChartComponent. The difference between the two notations is that with the first one you pass the varValue, which will be evaluated; whereas in the second notation the varName value IS 'varValue'.

And yes, the Angular docs are sometimes quite good. :)

I suggest you read this first https://angular.io/guide/ponent-interaction

发布评论

评论列表(0)

  1. 暂无评论