我正在开发一个项目,其中我使用了angularjs和mvc。我通过http post获取我的状态详细信息数据。现在我想在我的另一个控制器函数中使用我的数据。我已经传递了我的数据一个范围变量$ scope.Statuses并试图使用这个范围变量来获取我的另一个函数中的数据,但是它获得了null值。请建议如何实现这一点。
angularjs控制器
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);
}
});
};
}
答案 0 :(得分:1)
当您需要在控制器之间访问变量或函数时,AngularJS Services是您最好的朋友。最常用的服务是:'工厂','提供商','服务'
“Angular服务是使用依赖注入(DI)连接在一起的可替代对象。您可以使用服务在整个应用程序中组织和共享代码。”
有关AngularJS网站的更多信息
https://docs.angularjs.org/guide/services
更好的解释: