$scope.$watch … make it return a function on anoth

2019-08-01 21:34发布

问题:

I have a watch function that returns a value for 2 series of data in Highcharts. The example below shows how this would work within the controller:

$scope.$watch("trendCompany",function(){
   return $scope.chartConfig.series[0].data = 
            [-1.95,0.99,9.29,4.49,-1.50,-4.05,-7.05,10.13,-19.95,
            2.99,-14.55,-6.15,-20.25,-27.00,-26.10,-10.95,-10.95,5.30,
            6.06,11.12,10.13,11.96,22.13,6.74,4.67,1.43,0.27,4.20,-3.45,
            2.10,-1.65,1.92,1.85,0.00,1.97,-5.25,-3.30,1.67,3.87,6.27,1.89,
            2.27,0.59,-1.20,-5.85,-6.60,-2.25,-2.40,-2.85,-3.45,-0.15,2.63],
   $scope.chartConfig.series[1].data = 
            [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2];
});

$scope.chartConfig = {
    options: {
        chart: {
            type: 'line'
        }
    },
    series: [{
        data: []
    },{
        data: []
    }],
    title: {
        text: 'Hello'
    },

    loading: false
}

My question is.. how would $scope.chartConfig.series[i].data be equal to another function rather than this static array? I have a function that returns a result of data like so:

//Let a propertyExpr string define a traversal route into an object
$scope.dataResult = function(obj, propertyExpr) {
  return $parse(propertyExpr)(obj);
}

My first attempt was to go back to the watch function and do...

$scope.chartConfig.series[i].data = dataResult(obj, property);

Then, I tried...

$scope.chartConfig.series[i].data = function(){ return dataResult(obj,property); }

However, neither of these are working. How can I make the value of the series scope equal to something that is invoked by a function?

回答1:

If you define a function on the scope you also have to call it like that.

$scope.chartConfig.series[0].data = $scope.dataResult($scope.obj, $scope.property);