angularjs-另一个控制器的调用方法

时间:2015-06-09 10:21:44

标签: javascript angularjs twitter-bootstrap angularjs-scope

您好我正在开发一个项目,我有一个订阅/取消订阅通知按钮。我在此按钮上使用bootstrap模式单击并且如果用户选择确定我执行所需的操作。一切正常工作到最后为了刷新我的列表我需要调用方法我的另一个控制器。我尝试使用$ emit- $ on但无法使用它。请帮助如何从ModalInstanceCtrl控制器调用myIssuesController的GetAssignedIssues()方法。

angularjs代码

 //controller1
 var myIssuesController = function($scope, $sce, $http, cfpLoadingBar, deviceDetector, $filter, $modal, $log) {
$("#navMyIssues").addClass("active");
$scope.issueCommnets = null;
$scope.showComments = false;
$scope.Issues = [];
$scope.dateFormat = 'dd-MMM-yyyy';
$scope.dateTimeFormat = 'dd-MMM-yyyy h:mm:ss a';
$scope.selectedIssue = null;
$scope.statusName = null;
$scope.ProjectDetails = [];
$scope.selectedProject = null;
$scope.isVisible = false;
$scope.isVisibleReply = false;
$scope.notifiedMembers = null;
$scope.defaultProfileImagePath = "";

 //get all assigned issues
$scope.GetAssignedIssues = function() {
    alert('test');
    //$scope.issueCount = -1;
    $scope.issuesLoaded = false;
    $scope.issueDetailsLoaded = false;
    $scope.query = "";
    var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/GetAllAssignedIssues/';
    $http.post(url, []).success(function(data, status, headers, config) {
        if (data != '' || data.length == 0) {
            $scope.Issues = data;
            $scope.Issues = $filter('orderBy')($scope.Issues, 'CreatedOn', true);
            for (var count = 0; count < $scope.Issues.length; count++) {
                if ($scope.Issues[count].StatusName == "Pending") {
                    $scope.pendingIssueCount = $scope.pendingIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "In Progress") {
                    $scope.inprogressIssueCount = $scope.inprogressIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "Limitation") {
                    $scope.limitationIssueCount = $scope.limitationIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "Needs Research") {
                    $scope.needsresearchIssueCount = $scope.needsresearchIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "In Testing") {
                    $scope.intestingIssueCount = $scope.intestingIssueCount + 1;
                }
            }
            if (data.length != 0) {
                if ($scope.selectedIssue == null) {
                    $scope.selectedIssue = $scope.Issues[0];
                } else {
                    for (var count = 0; count < $scope.Issues.length; count++) {
                        if ($scope.Issues[count].Id == $scope.selectedIssue.Id) {
                            $scope.selectedIssue = $scope.Issues[count];
                        }
                    }
                }
            }
            $scope.issuesLoaded = true;
            $scope.showIssueDetails($scope.selectedIssue);
        } else {
            $scope.errors.push(data.error);
            //$scope.issueCount = -1;
        }
        if ($scope.isVisible == false) {
            $("#changedetailsbox").hide();
            $scope.isVisible = true;
        }
        if ($scope.isVisibleReply == false) {
            $("#postReplybox").hide();
            $scope.isVisibleReply = true;
        }
    }
    );
};

  $scope.$on("eventAssignedIssues", function (event,args) {
     alert('test1');
    $scope.GetAssignedIssues();
});
};

//controller 2
 var ModalInstanceCtrl = function ($scope, $modalInstance,modalHeader,modalBody,issueId,issueEmailId,$http) {
 $scope.modalHeader=modalHeader;
 $scope.modalBody=modalBody;
 $scope.issueId=issueId;
 $scope.issueEmailId=issueEmailId;
 $scope.ok = function () {
   if($scope.modalHeader=="Delete Issue")
   {

   }
   else if($scope.modalHeader=="Subscribe")
   {
     var issueNotification = { issueId: $scope.issueId, email: $scope.issueEmailId };
        var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/SubscribeIssueNotification/';
        $http.post(url, JSON.stringify(issueNotification)).success(function (data, status, headers, config) {
            if (data == "true" || data == true) {
                //$scope.GetAssignedIssues();
                 $scope.$root.broadcast("eventAssignedIssues",{});
            } else {
                $scope.errors.push(data.error);
            }
            $scope.showeditdesc = true;
        });
   }
   else if($scope.modalHeader=="Unsubscribe")
   {
   //for unsubscribing
        var issueId = $scope.issueId;
        var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/UnsubscribeIssueNotification/';
        $http.post(url, JSON.stringify({ issueId: issueId })).success(function (data, status, headers, config) {
            if (data == "true" || data == true) {
                //$scope.GetAssignedIssues();
                 $scope.$root.broadcast("eventAssignedIssues",{});
            } else {
                $scope.errors.push(data.error);
            }
            $scope.showeditdesc = true;
        });
   }
    $modalInstance.close();
};

$scope.cancel = function () {
    $modalInstance.dismiss('cancel');
};
 };

3 个答案:

答案 0 :(得分:3)

如果要在两个sibbling控制器之间进行通信,请使用$ rootScope.broacast() 请参阅此链接精彩说明 http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/

答案 1 :(得分:1)

  • 使用服务,工厂,价值将为您提供共享数据的目的
  • 使用$ emit和$ broadcast将为您完成工作,如下所示

      function firstCtrl($scope,$rootScope) {
    
        $scope.broadcast = function(bcMsg){
    
         $scope.broadcastMsg = bcMsg;
         $rootScope.$broadcast('broadC',$scope.broadcastMsg);
      }  
    
       $rootScope.$on('emitC',function(events,data){
          $scope.emitMsg = data;
       });
    }
    
    
     //second controller
      myApp.controller('secondCtrl',secondCtrl);
    
       //inject dependencies
      secondCtrl.$inject = ["$scope","$rootScope"];
    
     function secondCtrl($scope,$rootScope) {
    
     $scope.$on('broadC',function(events,data){
        $scope.broadcastMsg=data;
    });
    
         $rootScope.$on('emitC',function(events,data){
            $scope.emitMsg = data;
          });
    
        }
    

    http://jsfiddle.net/shushanthp/w4pwkkcq/

  • 使用指令在Using "require" in Directive to require a parent Controller

  • 中使用require也可以使用指令在控制器之间共享数据

答案 2 :(得分:0)

了解事件阅读本文 - enter link description here 我已经找到了@Shubham Nigam帮助的解决方案,所以非常感谢他 - 首先我们需要在两个控制器中定义$ rootScope并使用$ on和$ broadcast使用$ rootScope.Here是我的更新代码

回答

  //controller1
 var myIssuesController = function($rootScope,$scope, $sce, $http, cfpLoadingBar, deviceDetector, $filter, $modal, $log) {
$("#navMyIssues").addClass("active");
$scope.issueCommnets = null;
$scope.showComments = false;
$scope.Issues = [];
$scope.dateFormat = 'dd-MMM-yyyy';
$scope.dateTimeFormat = 'dd-MMM-yyyy h:mm:ss a';
$scope.selectedIssue = null;
$scope.statusName = null;
$scope.ProjectDetails = [];
$scope.selectedProject = null;
$scope.isVisible = false;
$scope.isVisibleReply = false;
$scope.notifiedMembers = null;
$scope.defaultProfileImagePath = "";

 //get all assigned issues
$scope.GetAssignedIssues = function() {
    alert('test');
    //$scope.issueCount = -1;
    $scope.issuesLoaded = false;
    $scope.issueDetailsLoaded = false;
    $scope.query = "";
    var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/GetAllAssignedIssues/';
    $http.post(url, []).success(function(data, status, headers, config) {
        if (data != '' || data.length == 0) {
            $scope.Issues = data;
            $scope.Issues = $filter('orderBy')($scope.Issues, 'CreatedOn', true);
            for (var count = 0; count < $scope.Issues.length; count++) {
                if ($scope.Issues[count].StatusName == "Pending") {
                    $scope.pendingIssueCount = $scope.pendingIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "In Progress") {
                    $scope.inprogressIssueCount = $scope.inprogressIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "Limitation") {
                    $scope.limitationIssueCount = $scope.limitationIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "Needs Research") {
                    $scope.needsresearchIssueCount = $scope.needsresearchIssueCount + 1;
                } else if ($scope.Issues[count].StatusName == "In Testing") {
                    $scope.intestingIssueCount = $scope.intestingIssueCount + 1;
                }
            }
            if (data.length != 0) {
                if ($scope.selectedIssue == null) {
                    $scope.selectedIssue = $scope.Issues[0];
                } else {
                    for (var count = 0; count < $scope.Issues.length; count++) {
                        if ($scope.Issues[count].Id == $scope.selectedIssue.Id) {
                            $scope.selectedIssue = $scope.Issues[count];
                        }
                    }
                }
            }
            $scope.issuesLoaded = true;
            $scope.showIssueDetails($scope.selectedIssue);
        } else {
            $scope.errors.push(data.error);
            //$scope.issueCount = -1;
        }
        if ($scope.isVisible == false) {
            $("#changedetailsbox").hide();
            $scope.isVisible = true;
        }
        if ($scope.isVisibleReply == false) {
            $("#postReplybox").hide();
            $scope.isVisibleReply = true;
        }
    }
    );
};

 $rootScope.$on('eventAssignedIssues', function (event, args) {
    $scope.GetAssignedIssues();
});
};

//controller 2
 var ModalInstanceCtrl = function ($rootScope,$scope, $modalInstance,modalHeader,modalBody,issueId,issueEmailId,$http) {
 $scope.modalHeader=modalHeader;
 $scope.modalBody=modalBody;
 $scope.issueId=issueId;
 $scope.issueEmailId=issueEmailId;
 $scope.ok = function () {
   if($scope.modalHeader=="Delete Issue")
   {

   }
   else if($scope.modalHeader=="Subscribe")
   {
     var issueNotification = { issueId: $scope.issueId, email: $scope.issueEmailId };
        var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/SubscribeIssueNotification/';
        $http.post(url, JSON.stringify(issueNotification)).success(function (data, status, headers, config) {
            if (data == "true" || data == true) {
                //$scope.GetAssignedIssues();
                 $rootScope.$broadcast('eventAssignedIssues');
            } else {
                $scope.errors.push(data.error);
            }
            $scope.showeditdesc = true;
        });
   }
   else if($scope.modalHeader=="Unsubscribe")
   {
   //for unsubscribing
        var issueId = $scope.issueId;
        var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/UnsubscribeIssueNotification/';
        $http.post(url, JSON.stringify({ issueId: issueId })).success(function (data, status, headers, config) {
            if (data == "true" || data == true) {
                //$scope.GetAssignedIssues();
                 $rootScope.$broadcast('eventAssignedIssues');
            } else {
                $scope.errors.push(data.error);
            }
            $scope.showeditdesc = true;
        });
   }
    $modalInstance.close();
};

$scope.cancel = function () {
    $modalInstance.dismiss('cancel');
};
 };
相关问题