0

I'm trying to keep things DRY so I moved my repetitive code into a function in my controller, but it's now saying that the function is undefined: Error: setFilterChoices is not defined

app.controller('MensCtrl', ['$scope', "MensEyeglass", "MensSunglass", "Color", "Shape", "Material", function($scope, MensEyeglass, MensSunglass, Color, Shape, Material) {

  $scope.init = function(category) {
    $scope.colors = [];
    $scope.shapes = [];
    $scope.materials = [];

    switch (category) {
    case "Eyeglasses":
      $scope.products = MensEyeglass.query({}, setFilterChoices(data));
      break;
    case "Sunglasses":
      $scope.products = MensSunglass.query({}, setFilterChoices(data));
      break;
    }
  }


  //it's defined here isn't it??
  $scope.setFilterChoices = function(data) {
    for (var i = 0; i < data.length; i++) {
      var product = data[i];

      if ($scope.shapes.indexOf(product.shape) == -1) {
        $scope.shapes.push(product.shape);
      }

      if ($scope.materials.indexOf(product.material) == -1) {
        $scope.materials.push(product.material);
      }


      for (var j = 0; j < product.products_colors.length; j++) {
        var product_color =  product.products_colors[j];
        if ($scope.colors.indexOf(product_color.color) == -1) {
          $scope.colors.push(product_color.color);
        }
      }
    }
  }

I was missing the function(data) callback as well. Here's the answer:

  $scope.products = MensSunglass.query({}, function(data) {
    $scope.setFilterChoices(data);
  });

1 Answer 1

3

its on the scope so use $scope.setFilterChoices(data) when u call it.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice. Thanks! How would I do it without using the scope?
var setFilterChoices = function(data) { ... }, or function setFilterChoices(data) { ... }. then call it as you originally did

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.