[英]How to access $scope object in factory using AngularJS?
I have serialize method for post , So riskAssessmentKey
is not part of $scope.topRiskDTO
but i pass the riskAssessmentKey value from $scope.riskAssessmentDTO.riskAssessmentkey
and now i am posting to factory but when i save all values are posting but riskAssessmentKey is coming undefined i dont know why.. 我有序列化方法post,所以
riskAssessmentKey
不是$scope.topRiskDTO
一部分,但是我从$scope.riskAssessmentDTO.riskAssessmentkey
传递了$scope.riskAssessmentDTO.riskAssessmentkey
值,现在我要投递到工厂,但是当我保存所有值都在投递但riskAssessmentKey不确定时我不知道为什么。
So far tried code.... 到目前为止尝试过的代码。
parentCtrl.js parentCtrl.js
$scope.addTopRisk = function(){
topRiskGridConfig.topRiskmodalWinConfig.title = 'Add top Risk';
$scope.viewTopRiskWin.setOptions(topRiskGridConfig.topRiskmodalWinConfig);
$scope.$broadcast('addTopRisk',$scope.riskAssessmentDTO.riskAssessmentKey);
};
childCtrl.js childCtrl.js
$scope.topRiskDTO = {};
$scope.issuePltDataSource = kendoCustomDataSource.getDropDownDataSource('RA_KY_CNCRN_IS_PLTFM');
$scope.$on('addTopRisk', function (s,id){
$scope.riskAssessmentDTO.riskAssessmentKey = id;
$scope.viewTopRiskWin.open().center();
$scope.submit = function(){
rcsaAssessmentFactory.saveTopRisk($scope.topRiskDTO,id).then(function(){
$scope.viewTopRiskWin.close();
});
};
});
factory.js factory.js
var serializeTopRisk = function (topRisk,id) {
var riskAssessmentKey = id;
var objToReturn = {
topRiskName: topRisk.topRiskName,
mitigationActivityDes: topRisk.mitigationActivityDes,
issuePltfLookUpCode: topRisk.issuePltfLookUpCode,
issueNo: topRisk.issueNo,
riskAssessmentKey: topRisk.riskAssessmentKey
};
if(topRisk.riskAssessmentKey){
objToReturn.riskAssessmentKey = topRisk.riskAssessmentKey;
}
return objToReturn;
};
saveTopRisk: function(topRisk,id) {
var request = serializeTopRisk(topRisk);
console.log('request payload', JSON.stringify(request));
console.log('ID :: ', id);
var endpoint = 'app/assessment/rest/addTopRisks';
return $http.post(endpoint, request);
}
You forgot to pass the id to the serializeTopRisk function. 您忘记了将ID传递给serializeTopRisk函数。
So you already pass the params correctly this this: 因此,您已经正确地通过了以下参数:
saveTopRisk: function(topRisk,id) {
var request = serializeTopRisk(topRisk);
But then serializeTopRisk should also get the id 但是然后serializeTopRisk也应该获取ID
var serializeTopRisk = function (topRisk, id) { // added the id over what you originally had
var riskAssessmentKey = $rootScope.riskAssessmentDTO.riskAssessmentKey; // drop this, use id instead
Don't use rootScope to pass data between the factory and the controller if you don't need to (it looks like you are already passing values to the factory from the controller by supplying it with object inputs, keep it that way and drop the rootScope usage from the factory). 如果不需要,不要使用rootScope在工厂和控制器之间传递数据(看起来您已经通过向控制器提供对象输入来将值从工厂传递给工厂,保持这种方式并删除rootScope的使用情况)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.