0

This is the first interaction with angularjs. it's first time. how can I call the function and get the items now in another controlers ?

I am loading CSV file using AngularJS

var Mymodule = angular.module('Mymodule', []);
Mymodule.factory('Items', ['$http', function($http){
      var Url   =  "data/ex.csv";
      var Items = $http.get(Url).then(function(response){
         return csvParser(response.data);
      });
      return Items;
    }]);

I need to get returned values to do some filtering on the data I have ?

  function doSomethingWithRows(rows) {
       //Mymodule.Items
      // How to call data here. 
    }

UPDATE Based on first answer

<script>
        var Mymodule = angular.module('Mymodule', []);
        Mymodule.factory('Items', ['$http', function($http){
              var Url   =  "data/ex.csv";
              var Items = $http.get(Url).then(function(response){
                 return csvParser(response.data);
              });
              return Items;
            }]);

    var $injector = angular.injector();
    $injector.invoke(['Items', function(Items){ console.log(Items) }]);


</script>

error:

Uncaught Error: Unknown provider: ItemsProvider <- Items 
2
  • depends where you placed doSomethingWithRows (in a controller, service, etc.?). In AngularJS you use dependency injection for such things. Commented May 3, 2014 at 12:59
  • I am running test now. the both and the only functions are inside <script></script> Commented May 3, 2014 at 13:00

2 Answers 2

1

I would extend your factory so you can call a function to get the results.

Mymodule.factory('Items', [
  '$http', 
  function($http){
      var Items;          

      this.loadItems = function loadItems() {     
          var Url   =  "data/ex.csv";
          $http.get(Url).then(function(response){
              Items = csvParser(response.data);
          });
      }

      this.returnItems = function returnItems() {
          return Items;
      }
}]);

Then in your controller:

Mymodule.controller("Controller", [
    "Items",
    function(Items){
        Items.loadItems();

        function doSomethingWithRows(rows) {
            var items = Items.returnItems();
        }
    }
]);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use $injector:

var $injector = angular.injector(["ng", "MyModule"]);
$injector.invoke(['Items', function(Items){ 
  /* do stuff */ 
  console.log(Items);  // e.g. print the Items
}]);

$injector is used to retrieve object instances as defined by some provider (e.g. your Items factory).

fiddle

1 Comment

small correction: you need to define the required modules when requesting the injector var $injector = angular.injector(["ng", "MyModule"]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.