简体   繁体   English

AngularJS为自定义指令的属性指定多个值

[英]AngularJS specifying multiple values for attribute of custom directive

I am creating a directive <chart> in AngularJS. 我正在AngularJS中创建指令<chart> I want to add a attributes x-series and y-series to it. 我想为其添加一个属性x-seriesy-series

A requirement is that these attributes should take multiple parameters like x-series="xData1 xdata2 xdata3" and y-series="ydata1 ydata2 ydata3" . 要求这些属性应采用多个参数,例如x-series="xData1 xdata2 xdata3"y-series="ydata1 ydata2 ydata3"

How do I resolve the different values of x-series and y-series attributes? 如何解决x系列和y系列属性的不同值? I can do it by splitting the values of scope.xSeries and scope.ySeries by space but I want to know that if Angular provides any better way to specify attribute with multiple values. 我可以通过按空格分割scope.xSeriesscope.ySeries的值来做到这scope.ySeries ,但我想知道Angular是否提供更好的方法来指定具有多个值的属性。

You can actually pass an array, for instance. 例如,您实际上可以传递一个数组。

<chart y-series="myArray"></chart>

//in controller

$scope.myArray = ['ydata1','ydata2'];

//in directive

scope:{
  'y-series': '=' // this will set up bi-directional data binding to the array
}

Maybe use keys for more information 也许使用键获取更多信息

x-series={key1:xData1, key2:xData2, key3:xData3}

This way you can distinguish using x-series.key1 and x-series.key2 这样,您可以使用x-series.key1x-series.key2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM