title: AngularJS : How to get returned value from factory
body: <p>I am very much new in AngularJS and I have the following problem:</p>
  
  <p>View:</p>
  
  <pre><code>&lt;div&gt;
      &lt;form role="form" name="loginForm" class="form-horizontal login-form"&gt;
          &lt;div class="form-group"&gt;
              &lt;p class="login-form-msg"&gt;Welcome : {{user.username}}&lt;/p&gt;
              &lt;label for="inputUserName" class="col-sm-2 control-label login-form-label"&gt;Base&lt;/label&gt;
              &lt;div class="col-sm-10"&gt;
                  &lt;input type="text" placeholder="Enter base" class="form-control" required ng-model="user.code"&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="inputUserName" class="col-sm-2 control-label login-form-label"&gt;Username&lt;/label&gt;
              &lt;div class="col-sm-10"&gt;
                  &lt;input type="text" placeholder="Enter name" class="form-control" required ng-model="user.username"&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label for="inputPassword" class="col-sm-2 control-label login-form-label"&gt;Password&lt;/label&gt;
              &lt;div class="col-sm-10"&gt;
                  &lt;input type="password" placeholder="Password" id="inputPassword" class="form-control" required ng-model="user.password"&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;div class="col-sm-offset-2 col-sm-10 login-form-button"&gt;
                  &lt;button class="btn btn-default" type="submit" ng-disabled="loginForm.$invalid" ng-click="login(user)"&gt;Sign in&lt;/button&gt;
              &lt;/div&gt;
              &lt;p class="login-form-msg"&gt;{{msgtxt}}&lt;/p&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Main js:</p>
  
  <pre><code>var myApp = angular.module('myApp', ['ngRoute']);
  
  myApp.config(['$routeProvider', function ($routeProvider) {
      $routeProvider.when('/login', {
          templateUrl: 'partials/login.html',
          controller:'loginCtrl'
      });
      $routeProvider.when('/welcome', {
          templateUrl: 'partials/welcome.html',
          controller:'loginCtrl'
      });
      $routeProvider.otherwise({ redirectTo: '/login' });
  }]);
  </code></pre>
  
  <p>I have the following factory:</p>
  
  <pre><code>myApp.factory('getURLService', function ($http, config) {
      return {
          getURL: function (user,scope) {
              $http.get(config.backend + "/Device/GetURLbyCode", {
                  params: { code: user.code }
              })
              .success(function (data) {
                  var url = '';
                  if (data.status == 'succ') {
                      url = data.Result;
                  }
                  else {
                      scope.msgtxt = data.msg;
                  }
                  return url;
              });
          }
      }
  });
  </code></pre>
  
  <p>I need to use the returned value from this factory in other factories.</p>
  
  <p>For example in my login factory:</p>
  
  <pre><code>myApp.factory('loginService', function ($http, $location, config) {
      return {
          login: function (user,url,scope) {
  
              $http.get(url + "/Device/Register", {
                  params: { userName: user.username, password: user.password }
              })
              .success(function (data) {
                  if (data.status == 'succ') {
                      $location.path('/welcome');
                  }
                  else {
                      scope.msgtxt = data.msg;
                  }
              });
          }
      }
  });
  </code></pre>
  
  <p>This is my controller:</p>
  
  <pre><code>myApp.controller('loginCtrl', function ($scope, getURLService, loginService) {
      $scope.msgtxt = '';
  
      $scope.login = function (user) {
          loginService.login(user,url,$scope); //call login service
      }
  });
  </code></pre>
  
  <p>What do I need to do in my controller to actually return the url and then send it to the login service or any other service (or even controller) in the future?</p>
  
  <p>Thanks in advance for your time and suggestion.</p>