gpt4 book ai didi

javascript - 如何使用在 ui-router 解析中发出 Ajax 请求的工厂/服务?

转载 作者:行者123 更新时间:2023-11-30 17:24:44 25 4
gpt4 key购买 nike

我正在为表单向导使用 ui-router( http://angular-ui.github.io/ui-router/site/#/api/ui.router ) 解决方案,我的状态配置如下所示:

$stateProvider
.state('TabsView', {
url: '/mcm/:mcmcid',
controller: 'TabsController',
templateUrl: 'ngapps/mcm/views/TabsView.html'
})
.state('TabsView.Campaign Info', {
url: '/campaign-info',
templateUrl: 'ngapps/mcm/views/Campaign Info.html',
resolve: {
campaignFactory: 'CampaignFactory',
campaignData: function($stateParams,campaignFactory) {
return campaignFactory.getCampaignInfo($stateParams.mcmcid).$service;
}
},
controller: 'CampaignInfoController'
});

对于状态“TabsView.Campaign Info”,我尝试通过调用 CampaignFactory 来解析 campaignData。 Campaign Factoy 的代码如下:

marketingCampaignModule.factory("CampaignFactory", ['AjaxFactory', '$q', function(AjaxFactory, $q) {
return {
getCampaignInfo: function(mcmcid) {
var result = {empty: true};
if (mcmcid > 0) {
var ajaxPromise = AjaxFactory.post("index.php/mcm/infosave/view", {mcmcid: mcmcid});
ajaxPromise.then(function(data) {
if (data['success']) {
if (data['params']) {
result = {'name': data['params']['name'], 'description': data['params']['description']};
}
}
return result;
});
}
}
};

}]);

解析中的 campaignData 未解析。但是我看到在控制台中调用了 CampaignFactory 的 getCampaignInfo 函数。我知道我做错了 promise 。请告诉我我做错了什么?

根据 Chandermani 的评论,我修改了工厂以返回一个 promise ,如下所示:

marketingCampaignModule.factory("CampaignFactory", ['AjaxFactory', '$q', function(AjaxFactory, $q) {
return {
getCampaignInfo: function(mcmcid) {
var deferred = $q.defer();
var result = {empty: true};
if (mcmcid > 0) {
var ajaxPromise = AjaxFactory.post("index.php/mcm/infosave/view", {mcmcid: mcmcid});
ajaxPromise.then(function(data) {
if (data['success']) {
if (data['params']) {
result = {'name': data['params']['name'], 'description': data['params']['description']};
}
}
deferred.resolve(result);
});
}else{
deferred.resolve(result);
}
return deferred.promise;
}
};

}]);

并修改resolve如下:

resolve: {
campaignFactory: 'CampaignFactory',
campaignData: function($stateParams,campaignFactory) {
campaignFactory.getCampaignInfo($stateParams.mcmcid).then(function(data){
return data;
});
}
}

还是不行。我做错了什么?

最佳答案

完成更新后,您只是在 resolve 中缺少 return 语句

 campaignData: function($stateParams,campaignFactory) {
return campaignFactory.getCampaignInfo($stateParams.mcmcid);
}

关于javascript - 如何使用在 ui-router 解析中发出 Ajax 请求的工厂/服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24492921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com