簡體   English   中英

如何將工廠函數用於不同的AngularJS控制器?

[英]How can i use factory function for different AngularJS controllers?

我有一個工廠功能,我想在三個不同的控制器中使用,如果要在控制器條件下使用工廠代碼塊,而在控制器的其他條件下要使用工廠代碼塊,我想使用。 如何使用工廠的任何幫助來完成此任務將不勝感激。

到目前為止,下面嘗試過的代碼...

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

                    };

好吧,我可能不在這里,但這是您要嘗試的嗎?

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

您也可以這樣解決此問題(如果我正確理解您的問題,則可能是首選方法):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM