不知何故,我无法将数据从工厂方法获取到 Controller 。我正在使用 javascript 远程处理,下面给出了工厂
function userRecordFetchFactory($rootScope) {
var custRec = {};
return {
checkRecordType : function(urlObject) {
function loadRecordType(err, records, event) {
if (err) {
displayReadingInformationErrorView();
} else if (records != null && records.length == 0) {
displayReadingInformationErrorView();
} else {
custRec = {
Name : records[0].get('Name'),
lat : records[0].get('Latitude__c'),
lon : records[0].get('Longitude__c'),
SiteStreet : records[0]
.get('SiteStreet__c'),
SiteCity : records[0].get('SiteCity__c'),
SiteCountryCode : records[0]
.get('SiteCountryCode__c'),
SitePostalCode : records[0]
.get('SitePostalCode__c'),
AddressID : records[0].get('AddressID__c'),
loaded : true
};
}
}
if (urlObject && urlObject.aid
&& urlObject.aid.startsWith(accPrefix)) {
objModel = new RemoteObjectModel.Account();
}
if (urlObject && urlObject.aid
&& urlObject.aid.startsWith(leadPrefix)) {
objModel = new RemoteObjectModel.Lead();
}
if (objModel) {
objModel.retrieve({
where : {
Id : {
eq : urlObject.aid
}
}
}, loadRecordType);
}
return custRec;
}
};
}
下面给出了我的 Controller 来访问数据
function LocatorInitController($document, $scope,
userRecordFetchFactory) {
console.log("inside the controller"+urlParams);
$scope.CustomerSite = {};
userRecordFetchFactory.checkRecordType(urlParams)
.then(function successData(data){
$scope.CustomerSite = data.data;
execGeoCoding();
});
我收到错误无法读取未定义的属性成功。在工厂中,方法 checkRecordType 有一个检索函数,它是一个 javascript 远程调用,并且该函数有一个对 loadrecordtype 的回调。
最佳答案
建议你用更简单的方式来编写你的工厂以便于阅读。你所有的嵌套导致你无法轻易看到整个事情
将所有可访问的成员放在顶部
// pass functions as references to object properties
// can easily see the whole factory object at top of the file
var custRec = {
checkRecordType : checkRecordType,
subscribe : subscribe
};
return custRec;
// move function declarations to the bottom and out of the way
function checkRecordType(){
/// do stuff
return stuff;
}
function loadRecordType(err, records, event) {
/// do stuff
return stuff;
}
function subscribe(scope, callback){
/// do stuff
return stuff;
}
关于javascript - 将数据从工厂传输到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39837970/