$ scope。$ watch ...让它在另一个范围内返回一个函数?

时间:2014-04-05 14:23:49

标签: javascript angularjs highcharts

我有一个watch函数,它返回Highcharts中2系列数据的值。下面的示例显示了它在控制器中的工作原理:

$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
}

我的问题是......怎么会 $ scope.chartConfig.series [i] .data等于另一个函数而不是这个静态数组?我有一个函数返回如下数据的结果:

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

我的第一次尝试是回到监视功能并做...

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

然后,我试过......

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

然而,这些都不起作用。如何使系列范围的值等于函数调用的值?

1 个答案:

答案 0 :(得分:2)

如果你在范围内定义一个函数,你也必须这样调用它。

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