如何在angularjs服务中使用$ scope [英] how to use $scope in angularjs service

查看:67
本文介绍了如何在angularjs服务中使用$ scope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将api响应存储在 $ scope 变量中.我已经写了我的主控制器.取而代之的是,我计划写入一个服务文件,并需要在所有控制器中使用它.现在,我正在所有控制器中重新编写代码.这样我的应用程序就会变慢.

I am storing my api response in a $scope variable. I have written in my main controller. Instead of that i am planning to write in one service file and need to use it in all controller. Right now i am re-writing the code in all controller. So that my application is getting slow.

MainController.js:

$scope.UserId = reply.split('UserId=').pop().toUpperCase();
apiService.getResponseData($scope.UserId).then(function(reply) {
  $scope.responseData = reply.data;
  $timeout(function() {
    reply.data.forEach(function(card) {
      if (card.Category == 'Apple') {
        console.log("Apple");
      } else if (card.Category == 'Google') {
        console.log("Google");
      } else if (card.Category == 'Microsoft') {
        console.log("Microsoft");
      } else if (card.Category == 'Amazon') {
        console.log("Amazon");
      }
    });
  });
});

这是我的mainController.如何在service.js文件中使用相同的代码.

This is my mainController. How can I use this same code in my service.js file.

CommonService.js:

app.service('commonService', ['$timeout', '$rootScope', 'apiService',
  function($timeout, $rootScope, apiService) {
    this.app = function($scope) {
      $scope.UserId = reply.split('UserId=').pop().toUpperCase();
      apiService.getResponseData($scope.UserId).then(function(reply) {
        $scope.responseData = reply.data;
        $timeout(function() {
          reply.data.forEach(function(card) {
            if (card.Category == 'Apple') {
              console.log("Apple");
            } else if (card.Category == 'Google') {
              console.log("Google");
            } else if (card.Category == 'Microsoft') {
              console.log("Microsoft");
            } else if (card.Category == 'Amazon') {
              console.log("Amazon");
            }

          });
        });
      });
      return app;
    };
  }
]);

$ scope 在服务文件中不起作用.与此相反,我该如何以其他方式使用.任何人都可以修改上面的代码.我想将此代码从服务使用到多个控制器.

$scope will not work in service file. Instead of this how can i use in other way. Can anyone please modify the above code. I want to use this code from service to multiple controllers..

推荐答案

可以使用,但不是很好的情况

Can be used but not a good case

MainController.js:

commonService.something($scope);

CommonService.js:

app.service('commonService', ['$timeout', '$rootScope', 'apiService',
    function ($timeout, $rootScope, apiService) {
        var something = function (scope) {
            apiService.getResponseData(scope.UserId).then(function (reply) {
                scope.responseData = reply.data;
                $timeout(function () {
                    reply.data.forEach(function (card) {
                        if (card.Category == 'Apple') {
                            console.log("Apple");
                        } else if (card.Category == 'Google') {
                            console.log("Google");
                        } else if (card.Category == 'Microsoft') {
                            console.log("Microsoft");
                        } else if (card.Category == 'Amazon') {
                            console.log("Amazon");
                        }

                    });
                });
            });
        };

        return {
            'something': something
        }
    }
]);

这篇关于如何在angularjs服务中使用$ scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆