从angular.js中的控制器调用Factory方法时出现“不是函数”错误

时间:2019-03-22 17:58:23

标签: angularjs

我正在尝试从控制器方法中调用angular.js中的工厂方法,并抛出错误“不是函数”

这是工厂的样子:

# Plot by country
library('ggplot2')
plot <- ggplot(group_by_country, aes(x = Group_name, y = Country_counts, fill = Countries_bygroup)) + 
  geom_bar(position = "fill",stat = "identity") +
  scale_y_continuous(labels = percent_format()) +
  xlab("Sample group") +
  ylab("")
plot

控制器看起来像:

function getData($q) {
  var getMessages = function(id) {
    var deferred = $q.defer();
    // deferred.notify('About to greet ' + name + '.');
    GTS2_FIN_MU_Dist_Pct_Controller.getUltimates(
      id,
      function(result, event) {
        if (event.status) {
          //      console.log(result);
          var obj = JSON.parse(result);
          deferred.resolve(obj);
        } else {
          deferred.reject("Error Processing the Request :  " + event.message);
        }
      },
      { escape: false }
    );
    return deferred.promise;
  };
  return {
    getMessages: getMessages,
  };
}

app.factory("getData", getData);

我还定义了应用程序。

app.controller("intlMassUpdateDistPctController", function($scope) 
        { // define angular controller
        $scope.ultimates = new Array; // define empty array to hold ultimate versions from VF remote action
        // Calls controller method Remote Action and passes the ultimateId, and handles the result by storing it inside $scope.ultimateVersions
        $scope.loadData = function(){
            //$j214('#dialogNew').show();
            var promise = getData.getMessages('');  //Calling factory method.. This is where U get the error
            promise.then(function(obj) {
            $scope.selectedForecastLocked = false;
            var JSONdata = JSON.parse(obj);
            $scope.ultimates = JSONdata.Ultimates;
            $scope.gridOptions.api.setRowData($scope.ultimates);
                    ........
            }, function(reason) {
                        alert('Failed: ' + reason);
                    }, function(update) {
                        alert('Got notification: ' + update);
                    });
            }
        });     

最后:

var app = angular.module('intlMassUpdateDistPct', ['agGrid','ui.bootstrap']);

有人可以帮助我确定丢失的内容吗。

1 个答案:

答案 0 :(得分:0)

尝试将getData工厂注入控制器:

app.controller("intlMassUpdateDistPctController", function($scope,getData){
   // your code...
})