Javascript angularjs在同一控制器的函数之间传递数据
我正在从事一个使用angularjs和mvc的项目。我正在通过http post获取我的状态详细信息数据。现在我想在控制器的另一个函数中使用我的此数据。我已在作用域变量$scope.status中传递了我的数据,并尝试使用此作用域变量获取另一个函数中的数据,但其值为空。请建议如何实现这一目标 angularjs控制器Javascript angularjs在同一控制器的函数之间传递数据,javascript,angularjs,controller,Javascript,Angularjs,Controller,我正在从事一个使用angularjs和mvc的项目。我正在通过http post获取我的状态详细信息数据。现在我想在控制器的另一个函数中使用我的此数据。我已在作用域变量$scope.status中传递了我的数据,并尝试使用此作用域变量获取另一个函数中的数据,但其值为空。请建议如何实现这一目标 angularjs控制器 var myIssuesController = function ($scope, $sce, $http, cfpLoadingBar, deviceDetector, $f
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);
}
});
};
}
var myissuecontroller=function($scope、$sce、$http、cfpLoadingBar、deviceDetector、$filter、$modal、$log){
$(“#navMyIssues”).addClass(“活动”);
$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.status=null;
$scope.issuesloadded=false;
$scope.issueDetailsLoaded=false;
$scope.modalHeader;
$scope.options;
var选择抗体;
//获取所有分配的问题
$scope.GetAssignedIssues=函数(){
//$scope.issueCount=-1;
$scope.issuesloadded=false;
$scope.issueDetailsLoaded=false;
$scope.query=“”;
var url=window.location.protocol+'/'+window.location.host+'/api/Issues'+'/getAllassignedisues/';
$http.post(url,[]).success(函数(数据、状态、标题、配置){
if(data!=''|| data.length==0){
$scope.Issues=数据;
$scope.Issues=$filter('orderBy')($scope.Issues,'CreatedOn',true);
$scope.Statuses=$scope.getIssueStatusDetails($scope.Issues[0]);
对于(变量计数=0;计数<$scope.Issues.length;计数++){
如果($scope.Issues[count].StatusName==“待定”){
$scope.pendingIssueCount=$scope.pendingIssueCount+1;
}
else if($scope.Issues[count].StatusName==“进行中”){
$scope.inprogressIssueCount=$scope.inprogressIssueCount+1;
}
else if($scope.Issues[count].StatusName==“限制”){
$scope.limitationIssueCount=$scope.limitationIssueCount+1;
}
else if($scope.Issues[count].StatusName==“需要研究”){
$scope.needsresearchIssueCount=$scope.needsresearchIssueCount+1;
}
else if($scope.Issues[count].StatusName==“测试中”){
$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=“边框底部:4px实心”+statusColor+”!重要;填充底部:5px;“;
}
}
如果(data.length!=0){
如果($scope.selectedIssue==null){
$scope.selectedIssue=$scope.Issues[0];
}否则{
对于(变量计数=0;计数<$scope.Issues.length;计数++)
{
如果($scope.Issues[count].Id=$scope.selectedIssue.Id){
$scope.selectedIssue=$scope.Issues[count];
}
}
}
}
$scope.issuesloadded=true;
$scope.showsissuedetails($scope.selectedIssue);
}
否则{
$scope.errors.push(data.error);
//$scope.issueCount=-1;
}
如果($scope.isVisible==false){
$(“#changedetailsbox”).hide();
$scope.isVisible=true;
}
如果($scope.isVisibleReply==false){
$(“#postReplybox”).hide();
$scope.isVisibleReply=true;
}
}
);
};
$scope.GetAssignedIssues();
$scope.getIssueStatusDetails=函数(问题){
var url=window.location.protocol+'/'+window.location.host+'/api/Issues'+'/GetIssueStatusDetails/';
$http.post(url,{ProjectId:issue.ProjectId}).success(函数(数据、状态、标题、配置){
if(data!=''|| data.length!=0){
$scope.selectedProject=数据;
$scope.Statuses=$scope.selectedProject.Statuses;
}否则{
$scope.errors.push(data.error);
}
});
};
}
当您需要在控制器之间访问变量或函数时,AngularJS服务是您最好的朋友。最常用的服务有:“工厂”、“供应商”、“服务”
“Angular服务是使用依赖项注入(DI)连接在一起的可替换对象。您可以使用服务在应用程序中组织和共享代码。”
更多关于AngularJS网站的信息
更好的解释:
这段代码很糟糕,使用服务,重构一下。并使用
调试器代码>或巴塔朗或类似的东西。