Marcos Gutierrez Marcos Gutierrez - 5 months ago 12
AngularJS Question

From factory to $scope . What is the right way?

I would like to know which is the correct way to initialize a variable with the data of a factory. Currently I do it this way, but does not convince me . I'm wrong? I 'm using Angular.

$scope.result = [];
factoryCities.then(function(data){
$scope.result = data;
});

Answer

A factory (singleton) is a service that aims to provide variables and/or methods.

in your controller:

...

MyFactory.var1 = $scope.var1;
$scope.resultOfSomethingDone = MyFactory.doSomething();

And if implements promises (asynchronously, return values (or exceptions) when they are done processing.) :

MyFactory.getData().then(function(data){
    $scope.dataResult = data;    
});