S
S
Sergey2015-06-18 10:17:33
Angular
Sergey, 2015-06-18 10:17:33

How to pass the name of the array?

I wanted to make a common service for several controllers with the same functionality.
Can I pass the name of the array oldArray? Those. do something like

app.controller('ConditionsController', ['$scope', 'Conditions', 'AddProduct',
             function($scope, Conditions, AddProduct) {
               
  $scope.conditions = Conditions.query();
  
  var oldArray = 'conditions';
  var  watchArray = 'conditionsAdded';
           
  $scope.addListCondition = function (currObj, key) {
            AddProduct.setList(currObj,oldArray,watchArray);
                                               };
   }]);

app.factory('AddProduct', ['$rootScope',	function($rootScope) {
    var List = [];
    /*
  / newObg - добавление значения
    oldArray - название массива
    watchArray - название для прослушивания в контроллере
 */
  var setList = function (newObj, oldArray, watchArray) {
          
    oldArray.push(newObj);
    $rootScope.$broadcast(watchArray);
    console.log(setList);
  };
  
  var getList = function(newArray){
           return newArray;     
      };
      
   return {
    getList: getList,
    setList : setList,
           };
  
}]);

Well, so that oldArray.push(newObj); instead of oldArray substitutedconditions.push(newObj);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question