1

I am working on a project in which I have used angularjs and mvc.I am getting my status details data by http post.Now I want to use my this data in my another function of controller.I have passed my data in a scope variable $scope.Statuses and tried to use this scope variable to get data in my another function but its getting null value.Please suggest how to achieve this.

angularjs controller

 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 = "";
$scope.pendingIssueCount = 0;
$scope.inprogressIssueCount = 0;
$scope.limitationIssueCount = 0;
$scope.needsresearchIssueCount = 0;
$scope.intestingIssueCount = 0;
$scope.Statuses = null;
$scope.issuesLoaded = false;
$scope.issueDetailsLoaded = false;
$scope.modalHeader;
$scope.options;
var selectedTab;
    //get all assigned issues
$scope.GetAssignedIssues = function () {
    //$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); 
            $scope.Statuses=$scope.getIssueStatusDetails($scope.Issues[0]);
            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;
                }                                               
                 var statusColor = "";

                     if ( $scope.Statuses[count]["Name"] == $scope.Issues[count].StatusName) {
                       statusColor =  $scope.Statuses[count]["ColorInHexa"];
                         $scope.Issues[count].IssueStatusStyle = "border-bottom: 4px solid " + statusColor + " !important;padding-bottom: 5px;";
                     }

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

$scope.getIssueStatusDetails = function (issue) {
        var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/GetIssueStatusDetails/';
        $http.post(url, { ProjectId: issue.ProjectId } ).success(function(data, status, headers, config) {
            if (data != '' || data.length != 0) {
                $scope.selectedProject = data;
                $scope.Statuses = $scope.selectedProject.Statuses;

            } else {
                $scope.errors.push(data.error);
            }
        });
};
}
rupinder18
  • 795
  • 1
  • 18
  • 43

1 Answers1

1

AngularJS Services is your best friend when you need access variables or functions between controllers. Most commonly used services are: 'factory', 'provider', 'service'

"Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app."

More information on AngularJS website

https://docs.angularjs.org/guide/services

Better explanation:

AngularJS: Service vs provider vs factory

Community
  • 1
  • 1
Taiwei Tuan
  • 430
  • 2
  • 12