Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

Using Services in AngularJS to return object value to controller

I am trying to read names from the JSON file. I first created without services using the only controller.But then to moved the business logic to services. I am not getting a clear idea that about how to return the $scope.mydata from service to the controller. Not getting any output please help. Thank you.

       <!DOCTYPE html>
       <html>
       <head>
         <title></title>
         <!-- Add angularjs -->
         <!-- Add controller script-->
         <script type="text/javascript">
            var app = angular.module('x2js', []);
            app.controller('ctrl',
               ['$scope','myService',function($scope,myService){
                $scope.data = function(){
                    myService.getData()
                }
            }]);
            app.service('myService', ['$http','$log', function($http,$log){
            this.getData = function(){
                $http({
                    url:"student.json",
                    method:"GET",
                    dataType: 'json',
                    contentType: "application/json"

                }).then(function(response){
                    $scope.myData = response.data.records;
                    $log.info($scope.post);
                    return($scope.myData);
                },function(response){
                    $log.error("Error occured");
                });
            }
        }])
       </script>
       </head>
       <body ng-app="x2js">
        <div ng-controller="ctrl">
            <table>
                <tr>
                    <td ng-repeat="x in Data">{{x.Name}}</td>
                </tr>
            </table>
        </div>
     </body>
     </html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use promise $http service return promise. You need to move your then functions into controller.

<!DOCTYPE html>
       <html>
       <head>
         <title></title>
         <!-- Add angularjs -->
         <!-- Add controller script-->
         <script type="text/javascript">
            var app = angular.module('x2js', []);
            app.controller('ctrl',
               ['$scope','myService', '$log',function($scope,myService, $log){

                   myService.getData().then(function(response){
                    $scope.data = response.data.records;
                    $log.info($scope.data);

                },function(response){
                    $log.error("Error occured");
                })
                }
            }]);
            app.service('myService', ['$http', function($http){
            this.getData = function(){
                retutn $http({
                    url:"student.json",
                    method:"GET",
                    dataType: 'json',
                    contentType: "application/json"

                });
            }
        }])
       </script>
       </head>
       <body ng-app="x2js">
        <div ng-controller="ctrl">
            <table>
                <tr>
                    <td ng-repeat="x in data">{{x.Name}}</td>
                </tr>
            </table>
        </div>
     </body>
     </html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...