How to include/inject functions which use $scope into a controller in angularjs?

后端 未结 3 1245
醉酒成梦
醉酒成梦 2020-12-31 10:23

I am trying to include a library of functions, held in a factory, into a controller. Similar to questions like this: Creating common controller functions

My

相关标签:
3条回答
  • 2020-12-31 10:34

    Your factory can't access your $scope, since it's not in the same scope.

    Try this instead:

    recipeApp.controller('recipeController', function ($scope, groceryInterface) {
    
        $scope.addToList = groceryInterface.addToList;
        $scope.clearList = groceryInterface.clearList;
        $scope.add       = groceryInterface.add;
        $scope.addUp     = groceryInterface.addUp;
    }
    
    recipeApp.factory('groceryInterface', function () {
    
        var factory = {};
    
        factory.addToList = function (recipe) {
            this.groceryList.push(recipe);
        }
    
        factory.clearList = function() {
            var last = this.prevIngredients.pop();
        }
    });
    

    Alternatively, you can try using a more object oriented approach:

    recipeApp.controller('recipeController', function ($scope, groceryInterface) {
    
        $scope.groceryFunc = new groceryInterface($scope);
    }
    
    recipeApp.factory('groceryInterface', function () {
    
        function Factory ($scope) {
    
            this.$scope = $scope;
        }
    
        Factory.prototype.addToList = function (recipe) {
            this.$scope.groceryList.push(recipe);
        }
    
        Factory.prototype.clearList = function() {
            var last = this.$scope.prevIngredients.pop();
        }
    
        return Factory;
    });
    
    0 讨论(0)
  • 2020-12-31 10:40

    This isn't the exact answer for this question, but I had a similar issues that I solved by simply passing $scope as an argument to a function in my factory. So it won't be the normal $scope, but $scope at the time the function in the factory is called.

    app.controller('AppController', function($scope, AppService) {
    
    
      $scope.getList = function(){
    
        $scope.url = '/someurl'
    
        // call to service to make rest api call to get data
    
        AppService.getList($scope).then(function(res) {
          // do some stuff 
    
        });
      }
    
    });
    
    
    app.factory('AppService', function($http, $q){
      var AppService = {
    
        getList: function($scope){
          return $http.get($scope.url).then(function(res){
            return res;
          });
        },
    
      }
    
      return AppService;
    });
    
    0 讨论(0)
  • 2020-12-31 10:57

    You cannot use $scope in a factory as it is not defined. Instead, in your factory functions change the properties of the object the factory is returning, e.g.

    factory.addToList = function (recipe) {
        this.groceryList.push(recipe);
    }
    

    these will then get passed on to your $scope variable

    $scope.addToList = groceryInterface.addToList;
    // ... = groceryInterface.addToList(); would assign to `$scope.addToList` what is returned, instead of the function itself. 
    
    0 讨论(0)
提交回复
热议问题