从服务角度将数据传递给控制器

时间:2016-09-22 01:31:41

标签: angularjs

我有一个给定的服务

angular.module('LocatorInitApp').factory('userRecordService',userRecordService);

function userRecordService(){
        var custRec;
        this.checkUserType = function(urlParams){
            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(urlObject == null || urlObject.aid == null || urlObject.length <= 3 || objModel == null){
          displayReadingInformationErrorView();
      }
      if(objModel){
          objModel.retrieve({ where: { Id: { eq: urlObject.aid}} }, loadRecordType);
      }

      function  loadRecordType(err,records,event){
          if(err){
              displayReadingInformationErrorView();
              }
              else if(records != null && records.length == 0 ){
                  displayReadingInformationErrorView();
                  }
                  else {
                      console.log(records[0].get('Name')+"Record Exist");
                       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
                           };
                           siteInformationReadingSuccessfulView();
                       }
                  }
            return custRec;
        },

        this.getUser = function (){
            return custRec;
        }
    }

我正在尝试访问我的控制器中的custRec,但我无法这样做。有人能告诉我一种访问custRec的方法。或者,如果我应该删除工厂并改为使用服务模式?

2 个答案:

答案 0 :(得分:0)

您尚未从服务公开custRec变量返回一个对象。

解决方案:在服务结束之前(userRecordService函数),写下:

`

return {
 custRec: custRec
}

`

现在在控制器中注入userRecordService,然后可以使用userRecordService.custRec语句在控制器中使用custRec变量。

答案 1 :(得分:0)

首先,您使用“this”来绑定工厂对象,这是不推荐的。您可以使用 .service()而不是 .factory()

你可以试试下面的代码来解决你的问题。它可能对你有用。

angular.module('LocatorInitApp').factory('userRecordService', userRecordService);

function userRecordService() {
    var custRec;
    this.checkUserType = function(urlParams) {
            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 (urlObject == null || urlObject.aid == null || urlObject.length <= 3 || objModel == null) {
                displayReadingInformationErrorView();
            }
            if (objModel) {
                objModel.retrieve({
                    where: {
                        Id: {
                            eq: urlObject.aid
                        }
                    }
                }, loadRecordType);
            }

            function loadRecordType(err, records, event) {
                if (err) {
                    displayReadingInformationErrorView();
                } else if (records != null && records.length == 0) {
                    displayReadingInformationErrorView();
                } else {
                    console.log(records[0].get('Name') + "Record Exist");
                    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
                    };
                    siteInformationReadingSuccessfulView();
                }
            }
            return custRec;
        },

        this.getUser = function() {
            return custRec;
        }

    //returning factory object..

    return {
        checkUserType: checkUserType,
        getUser: getUser
    };
}