javascript - 我如何为不同的 AngularJS Controller 使用工厂函数?

标签 javascript angularjs

我有一个工厂函数,我想在三个不同的 Controller 中使用,我想在 Controller 的 if 条件内使用 if 工厂代码块,而我想在 Controller 的 else 条件下使用 else 工厂代码块。如何使用工厂完成此任务任何帮助将不胜感激。

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

工厂.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);
  }
});

关于javascript - 我如何为不同的 AngularJS Controller 使用工厂函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801002/

相关文章:

javascript - jQuery 在 src 上附加带有 url 的 img 元素在 IE 和 FF 中不起作用

javascript - 在 native Javascript 中将一些 innerHTML 添加到 div

javascript - 如何从这个 d3.js layout.tree 中获取树祖先和树后代的列表?

javascript - 使用 angularjs 对 Json 文件进行 Ng 重复?

javascript - 仍然对 jQuery 中的事件和事件处理程序感到困惑

javascript - 在 Express 中渲染包含 javascript src 文件的 HTML

javascript - 存储和解析 CSS/JS

javascript - 当用户停止在 AngularJS 中输入时执行函数

angularjs - 如何在 Protractor 中使用 Jasmine 和 CucumberJS

javascript - Angular + ui-grid + 计算列总数