0

コードの一部が期待どおりに動作しない理由をデバッグする簡単なテストを試みています。

という名前のコントローラーtestCtrlと service がありますmyService。このサービスでは、Parse からデータを取得しようとしています。データを取得したら、このデータをフロントエンド html にロードしようとしています。

コードは次のとおりです。

app.controller('testCtrl', function($scope,myService) {
var currentUser = Parse.User.current();

$scope.username = currentUser.getUsername();
$scope.test = "ths";
var promise1 = myService.getEvaluatorData();
promise1.then(function(response){
    $scope.results2 = response;
    console.log(response);
    });
});

app.factory('myService', function(){
 return {
   getAllData: function($scope){
      getEvaluatorData($scope);
   },

   getEvaluatorData: function(){

       var evaluators = Parse.Object.extend("Evaluators");
       query = new Parse.Query(evaluators);

       return query.find({
          success: function(results){
            angular.forEach(results, function(res){

                console.log("Looped"); //this is just to verify that the then call below is executed only after all array objects are looped over.
            });

          } ,
          error: function(error){

          }
       });
   }
  }
});

データを表示したいHTMLコードは次のとおりです。

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-controller="testCtrl">
{{test}}

{{results2}}
</body>
</html>

html に読み込まれresults2ません。

これがコンソールログです。

testParse.js:46 This is done
2015-07-10 15:28:32.539testParse.js:46 This is done
2015-07-10 15:28:32.540testParse.js:46 This is done
2015-07-10 15:28:32.541testParse.js:46 This is done
2015-07-10 15:28:32.542testParse.js:56 Returning result as promised [object Object],[object Object],[object Object],[object Object]
4

2 に答える 2

0

returnサービスからデータを取得し、コントローラーに渡す必要があります。例:

app.controller('testCtrl', function ($scope, myService) {
    var currentUser = Parse.User.current();
    $scope.username = currentUser.getUsername();
    $scope.test = "this";
    myService.getEvaluatorData().then(function (response) {
        console.log('response', response);
        $scope.results2 = response;
    });
});

app.factory('myService', function ($http, $q) {
    return {
        getEvaluatorData: function () {
            var evaluators = Parse.Object.extend("Evaluators");
            var query = new Parse.Query(evaluators);

            return query.find({
                success: function (results) {
                    return results;
                },
                error: function (error) {
                   return error;
                }
            });
        }
    }
});

また、メソッドを直接this.getEvaluatorData($scope)呼び出すことができる場合、私の意見では呼び出しても意味がありませんgetEvaluatorData

于 2015-07-11T06:54:18.580 に答える
0

$scope.result2 の代わりに $rootScope.result2 を使用します

于 2015-11-09T11:29:35.713 に答える