简体   繁体   中英

How can i use factory function for different AngularJS controllers?

I have a factory function that i want to use in three different controllers, I want to use if factory code block inside if condition of controller and else factory code block i want to use in else condition of controller. How to achieve this task using factory any help will be appreciated.

So far tried code below...

Factory.js

        angular.module("App").factory('geoTreeFactory', function() {
            return {
  getTreeCheck: function (geoLocation) {
      if (geoLocation.id === 5657){
        $.each(geoLocation.parent(),function(index,location) {
          if (location.id !== geoLocation.id) {
            $.each(location._childrenOptions.data.items,function(index,child){
              var disableChildId = 'disabled' + child.id;
              var model = $parse(disableChildId);
                model.assign($scope, true);
            })
            var disableItemId = 'disabled' + location.id;
            var model = $parse(disableItemId);
            model.assign($scope, true);
          }
        });
      }
      // If a child is checked Disable the parents
      try {
          var parent = geoLocation.parent().parent();
          var disableParentId = 'disabled' + parent.id;
          var parentModel = $parse(disableParentId);
          parentModel.assign($scope, true);
      } catch (err) {
     // Ignore since this happens when a node is selected which has no children 
        }
     //Expand and collapse tree on parent check so childrens can be disabled.
      $scope.riskInPrcsgeoLocationTree.expand($scope.riskInPrcsgeoLocationTree.findByText(geoLocation.text));
      $scope.riskInPrcsgeoLocationTree.collapse($scope.riskInPrcsgeoLocationTree.findByText(geoLocation.text));
      //If the parent item is checked, disable all the children
      if(geoLocation.items) {
          $.each(geoLocation.items,function(index,location) {
            var disableItemId = 'disabled' + location.id;
            var model = $parse(disableItemId);
            model.assign($scope, true);
          });
      } 
      },

      //Else block function if ocnditions are false
      getTreeUncheck: function(geoLocation) {
        if (geoLocation.id === 5657){
          var getParent = geoLocation.parent();
          $.each(geoLocation.parent(),function(index,location) {
            if (location.id !== geoLocation.id) {
              $.each(location._childrenOptions.data.items,function(index,child){
                var disableChildId = 'disabled' + child.id;
                var model = $parse(disableChildId);
                  model.assign($scope, false);
              })
              var disableItemId = 'disabled' + location.id;
              var model = $parse(disableItemId);
              model.assign($scope, false);
            }
          });
        }
        // If child is unchecked Enable the parent
        try {
          var parent = geoLocation.parent().parent();
          var checkedChildrens = [];
          for (var i=0; i<selectedRiskGeoLocations.length; i++){
            var checkNodes = selectedRiskGeoLocations[i];
            checkedChildrens.push(checkNodes);
          }
          if (checkedChildrens.length === 0){
            var disableParentId = 'disabled' + parent.id;
            var parentModel = $parse(disableParentId);
            parentModel.assign($scope, false);
          };
      }
        catch (err) {
          // Ignore since this happens when a node is selected which has no children 
        }
        //If the parent item is unchecked,  enable the childrens
        if(geoLocation.items){
            $.each(geoLocation.items,function(index,location){
              var disableItemId = 'disabled' + location.id;
              var model = $parse(disableItemId);
              model.assign($scope, false);
            });
        }
      }
};

Controller.js

var selectedCtlGeoLocations = [];
                    var selectedCtlGeoLocationIds = [];
                    $scope.populateControlInPrcsGeoLoction = function(geoLocation) {
                        var pos = $.inArray(geoLocation.text,selectedCtlGeoLocations);
                        if (pos < 0) {
                            selectedCtlGeoLocations.push(geoLocation.text);
                            selectedCtlGeoLocationIds.push(geoLocation.id);

                            // If the parent item is checked, disable all the
                            // children
                         geoTreeFactory.getTreeIfBlock();

                        } else {
                            selectedCtlGeoLocations.splice(pos, 1);
                            selectedCtlGeoLocationIds.splice($.inArray(
                                    geoLocation.id, selectedCtlGeoLocationIds),
                                    1);

                            // If the parent item is unchecked, enable the
                            // children
                            geoTreeFactory.getTreeElseBlock();
                        }

                    };

Alright, I could be way off here, but is this what you're trying to do?

app.factory('myservice', function(){
  return {
    ifBlock: function(){
      // code to run if condition is true
    },
    elseBlock: function(){
      // code to run if condition is false
    }
  }
});

app.controller('main', function(myservice){
  $scope.myevent = function(geoLocation){
    if(condition){
      myservice.ifBlock(geoLocation);
    } else {
      myservice.elseBlock(geoLocation);
    }
  }
});

You could also solve this problem like this (and if I understand your problem correctly, this is probably the preferred method):

app.service('myservice', function(){
  return {
    go: function(geoLocation){
      if(condition){
        // code to run if condition is true
      } else {
        // code to run if condition is false
      }
    }
  }
});

app.controller('main', function(myservice){
  $scope.myevent = function(geoLocation){
    myservice.go(goeLocation);
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM