如何为不同的AngularJS控制器使用工厂功能?

时间:2015-04-22 14:39:15

标签: javascript angularjs

我有一个工厂功能,我想在三个不同的控制器中使用,如果工厂代码块内部如果控制器的条件和工厂代码块我想在控制器的其他条件下使用我想使用。如何使用工厂任何帮助来完成这项任务将不胜感激。

到目前为止尝试了以下代码......

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();
                        }

                    };

0 个答案:

没有答案