将局部变量的值获取到AngularJS中的控制器中

时间:2021-05-20 23:39:28

I have a JS file containing a controller and other functions structured like this:

我有一个包含控制器的JS文件和其他结构如下:

class RandomCtrl {
    constructor(randomService) {
    this.randomService = randomService;
    ...
    }

    $onInit() {
        getData.call(null, this);
    }

    ...

}

function getData(RandomCtrl) {
    ...
}


function getChart(data) {
    if (!data) {
        return;
    }

    const chartOptions = getWeekHourlyOptions(data);

    const allCols = [].concat(chartOptions.dataColumns);
    allCols.push(["x"].concat(_.map(chartOptions.xAxis, element => moment(element).valueOf())));
    const xVals = xAxisValues(chartOptions.xAxis);

    ...
}

...

RandomCtrl.$inject = ['randomService'];

export const Random = {
    bindings: {
        data: '<',
        siteNames: '<'
    },
    templateUrl: randomPageHtml,
    controller: RandomCtrl
};

I want to get the value of allCols from getChart() into the Controller.

我想从getChart()中获取allCols的值到Controller中。

I've tried to add it in $onInit like this.allCols = getData.allCols but it doesn't work. Probably not the right way.

我试图在$ onInit中添加它像this.allCols = getData.allCols但它不起作用。可能不是正确的方式。

Any solution to this?

对此有何解决方案?

1 个解决方案

#1


0  

  1. Declare allCols as a global variable and populate the data in getChart()
  2. 将allCols声明为全局变量并在getChart()中填充数据
  3. send allCols in getChart() as return value so that whenever this method is called the return value can be stored into another variable and can be consumed
  4. 将getChart()中的allCols作为返回值发送,这样无论何时调用此方法,返回值都可以存储到另一个变量中并可以被消耗

#1


0  

  1. Declare allCols as a global variable and populate the data in getChart()
  2. 将allCols声明为全局变量并在getChart()中填充数据
  3. send allCols in getChart() as return value so that whenever this method is called the return value can be stored into another variable and can be consumed
  4. 将getChart()中的allCols作为返回值发送,这样无论何时调用此方法,返回值都可以存储到另一个变量中并可以被消耗