$scope : i want to move some methods to common factory, which is used in all controllers.

42 views
Skip to first unread message

Yogesh N

unread,
Jun 22, 2015, 8:31:57 AM6/22/15
to ang...@googlegroups.com
angular.module('accguru')
 
.controller('salesOrderController', function ($scope, $state, $filter, databasefactory, commonFactory) {

   
var tenantId = metaDataLocalStorage.getTenantId();
   
var branchId = metaDataLocalStorage.getBranchId();
   
var taxCategoryFetchQuery = commonFactory.getTaxCategoryFetchQuery(tenantId, branchId);

   
var init = function() {
     getAllSimpleTaxes
();
   
};

   init
();

   
function getAllSimpleTaxes() {
      databasefactory
.retriveData(taxCategoryFetchQuery).then (function (results) {
        $scope
.taxes = [];
       
if (results.rows.length > 0) {
         
for (var i = 0; i < results.rows.length; i++) {
            $scope
.taxes.push(results.rows.item(i));
         
}
       
}
     
}, function (errorMsg) {
        showMessage
(errorMsg);
     
});
   
}

   
function showMessage(message) {
      $scope
.message = message;
      $scope
.data = {
        showMessage
: true
     
};
   
}
})

.controller('receiptController',function($scope, $state, $filter, databasefactory, commonFactory) {

   var tenantId = metaDataLocalStorage.getTenantId();
   
var branchId = metaDataLocalStorage.getBranchId();

   var glAcctOrgFetchQuery = commonFactory.getGlAcctOrgFetchQuery(tenantId, branchId);

   var init = function() {
       getPaymentModeGlAcctOrgList();
   };

   init();

   function getPaymentModeGlAcctOrgList() {
      databasefactory.retriveData(glAcctOrgFetchQuery).then(function(results) {
        $scope.paymentModeGlAcctOrgArr = [];
        if (results.rows.length > 0) {
          for (var i = 0; i < results.rows.length; i++) {
            $scope.paymentModeGlAcctOrgArr.push(results.rows.item(i));
          }
        }
      }, function(errorMsg) {
        showMessage(errorMsg);
      });
   }

   function showMessage(message) {
      $scope.message = message;
      $scope.data = {
        showMessage: true
      };
   }
});


In the above controller code, i want to move showMessage(message) function to the commonFactory.
Since i am using $scope, i am not able to move to commonFactory. can anyone please help me out in solving this issue.

Here is the commonFactory code:

angular.module('commonModule')
.factory('commonFactory', function(queriesTaxCategory, queriesGlAccountOrganization) {
  var commonFactory = {};

  commonFactory.getTaxCategoryFetchQuery = function(tenantId, branchId) {
    var taxCategoryFetchQuery = 'SELECT * FROM TaxCategory WHERE tenantId =' + tenantId +
         ' AND branchId =' + branchId;
    return taxCategoryFetchQuery;
  };

  commonFactory.getGlAcctOrgFetchQuery = function(tenantId, branchId) {
    var glAcctOrgFetchQuery = 'SELECT * FROM GlAccountOrganization WHERE tenantId =' + tenantId +
    ' AND branchId =' + branchId;
    return glAcctOrgFetchQuery;
  };
  return commonFactory;
});

Here is the html code, where i am displaying error message if data.showMessage is true.

<ion-view title="New Receipt">
  <div class="bar bar-loading bar-assertive" ng-if="data.showMessage">
     {{message}}
  </div>

  <ion-content>
    <div>
    ------
    -----
    -----
    <div>
  </ion-content>
</ion-view>

Alain Chautard

unread,
Jun 22, 2015, 1:31:07 PM6/22/15
to ang...@googlegroups.com
What if you pass your controller's $scope as a parameter of your showMessage function. That way the factory method would look like this:

function showMessage(localScope, message) {
      localScope.message = message;
      localScope.data = {
        showMessage: true
      };
   }

And in you controller you would call it: commonFactory.showMessage($scope, message);

Yogesh N

unread,
Jun 23, 2015, 2:30:21 AM6/23/15
to ang...@googlegroups.com

Hi Alain,

Thankyou for your help. I followed the procedure what you have replyed and issue is solved.



Regards,
Yogesh

Alain Chautard

unread,
Jun 23, 2015, 1:35:33 PM6/23/15
to ang...@googlegroups.com
Glad to read that!

Alain Chautard

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/WjLN9SSfbDE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages