如何从Angular控制器修改数据库查询的结果

时间:2016-02-04 21:33:15

标签: angularjs controller meanjs

我是Angular和MEAN.js的新手,我正试图弄清楚如何在数据到达视图之前操纵从我的控制器(客户端)返回的数据。我在我的控制器中有一个函数,它对数据库进行查询。

    $scope.getDataPoints = function() {
        $scope.datapoints = Datapoints.query();
        console.log($scope.datapoints.length);
        console.log($scope.datapoints);

        for (var i = 0; i < $scope.datapoints.length; i++) {
           console.log($scope.datapoints[i].thevalue + ",");
            console.log($scope.datapoints[i].variant + ",");
        }

    };

正如预期的那样,查询返回一个promise,因此循环不会执行。如何捕获,操作(使用lodash)然后将数据提供给视图?

1 个答案:

答案 0 :(得分:0)

如果它返回一个承诺

 $scope.getDataPoints = function() {
   Datapoints.query()
     .then(function(response) {
       // Don't know what this resolves with, assuming response.data is what you would want
       $scope.datapoints = response.data;

       console.log($scope.datapoints.length);
       console.log($scope.datapoints);

       for (var i = 0; i < $scope.datapoints.length; i++) {
         console.log($scope.datapoints[i].thevalue + ",");
         console.log($scope.datapoints[i].variant + ",");
       }
     });
 }