简体   繁体   English

从Angularjs工厂回调

[英]Callback from Angularjs factory

I want to get a callback from a factory. 我想从工厂得到回调。 If I understand correctly, the callback should be in the deepest nested function (ie under var myResult = $filter('filter')(myData, {id:exnum})[0]; , but I get "TypeError: callback is not a function". 如果我理解正确,则回调应该位于最深层的嵌套函数中(即,在var myResult = $filter('filter')(myData, {id:exnum})[0]; ,但出现“ TypeError:callback is not功能”。

My factory calls another factory, gets a value and injects it into a third one for the final result. 我的工厂呼叫另一个工厂,获取一个值并将其注入到第三个工厂中以得到最终结果。 This final result logs correctly to console, but I cannot callback to the controller. 此最终结果正确记录到控制台,但是我无法回调到控制器。

Any feedback would be appreciated. 对于任何反馈,我们都表示感谢。

angular.module('resourceFetch', [])
.factory('ResourceFetch', ['JsonService', 'UserProgress', '$filter', function(JsonService, UserProgress, $filter) {

    var resourceResult = {};
    resourceResult.getResource = function(callback){

      UserProgress.getProgress(function(exnum, callback) {
        JsonService.get(function(data){
        var myData = [];
        var myData = data.exercises;
        var myResult = [];
        var myResult = $filter('filter')(myData, {id:exnum})[0];
        console.log(myResult) // <- this displays correctly
        callback(myResult); // <- "TypeError: callback is not a function"
        });
     });
   //callback(myResult); <- here "myResult is not defined"
   };
    return resourceResult;
}]);

This is the controller: 这是控制器:

myApp.controller('ResourceFetchTest', function($scope, ResourceFetch) {

    $scope.myresults = ResourceFetch.getResource(function(obj1){
       console.log('obj1 is ' + obj1);
       $scope.MyData = obj1;
       $scope.MySelectedData = obj1.string1;
    }); 
});

Sorry, my previous answer was not looking at your code properly, your biggest issue here is that you are trying to pass services within services and it makes your code hard to follow. 抱歉,我先前的回答是未正确查看您的代码,这里最大的问题是您试图在服务中传递服务,这使您的代码难以理解。

What you should do is inject all of your services to your controller module and then you can do something along the lines of this. 您应该做的是将所有服务注入到控制器模块中,然后您可以执行类似的操作。

myApp.controller('ResourceFetchTest', function($scope, ResourceFetch) {

    $scope.dataForProgress = "Bind whatever data in needed to get your progress";

    $scope.dataForJson = UserProgress.getProgress(dataForProgress);

    $scope.myResults = JsonService.get(dataForJson);

});

Depending on what each service does and what it calls it is possible you are also making Async calls in which case I would recommend looking into the $q directive angular provides. 根据每个服务的功能以及调用的内容,您可能还会进行异步调用,在这种情况下,我建议您查看$ q指令angular提供。

You could use a promise to return the object 您可以使用一个承诺来返回对象

Something like: 就像是:

angular.module('resourceFetch', [])
.factory('ResourceFetch', ['JsonService', 'UserProgress', '$filter','$q', function(JsonService, UserProgress, $filter,$q) {

    var resourceResult = {};
    resourceResult.getResource = function(){
    var defer = $q.defer();
      UserProgress.getProgress(function(exnum) {
        JsonService.get(function(data){
        var myData = [];
        var myData = data.exercises;
        var myResult = [];
        var myResult = $filter('filter')(myData, {id:exnum})[0];
        console.log(myResult) // <- this displays correctly
        defer.resolve(myResult); 
        });
     });
   return defer.promise;
   };
    return resourceResult;
}]);

and in the controller: 并在控制器中:

myApp.controller('ResourceFetchTest', function($scope, ResourceFetch) {

    $scope.myresults = ResourceFetch.getResource().then(function(obj1){
       console.log('obj1 is ' + obj1);
       $scope.MyData = obj1;
       $scope.MySelectedData = obj1.string1;
    }); 
});

here's the documentation on promises: https://docs.angularjs.org/api/ng/service/ $q 这是关于诺言的文档: https : //docs.angularjs.org/api/ng/service/ $ q

Let me know if you have questions I had the simular problem today and fixed it this way 让我知道您是否有今天遇到的类似问题,并通过这种方式解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM