0

i followed the directions from this post:

Redirect using AngularJS

here is my user.js service:

'use strict';

staticApp.factory('user', ['$scope', '$http', '$q', '$location', function ($scope, $http, $q, $location) {
  $http.defaults.useXDomain = true;

  var host = 'http://' + window.location.hostname + ':5001';

  return {
    current: function() {
      var d = $q.defer();
      var user = $http({method: 'GET', url: host + '/api/user.json'});
      user.success(function(resp) {
        console.log('SUCCESS! logged in!');
        d.resolve(resp.user);
      });
      user.error(function(resp) {
        console.log('not logged in!');
        var login_url = 'http://path/to/login/';
        scope.$apply(function() { $location.path(login_url); });
      });
      return d.promise;
    }
  };
}]);

i am running a python flask server for the app's API.

this piece of code executes fine when i have authenticated already:

user.success(function(resp) {
  console.log('SUCCESS! logged in!');
  d.resolve(resp.user);
});

however, when i have not authenticated and this piece of code executes:

user.error(function(resp) {
    console.log('not logged in!');
    var login_url = 'http://path/to/login/'
    scope.$apply(function() { $location.path(login_url); });
 });

i get this error:

Error: Unknown provider: $scopeProvider <- $scope <- user <- navigationDirective
Community
  • 1
  • 1
SeanPlusPlus
  • 8,663
  • 18
  • 59
  • 84
  • 1
    Do you need a $ in front of scope.$apply(...) ? So $scope.$apply... – Foo L Apr 26 '13 at 00:55
  • if i set the call to scope.apply i still get the err: Error: Unknown provider: $scopeProvider <- $scope <- user <- navigationDirective – SeanPlusPlus Apr 26 '13 at 01:13
  • if i don't pass the $scope the function, i get this err: ReferenceError: scope is not defined – SeanPlusPlus Apr 26 '13 at 01:14
  • 1
    I meant for you to try $scope.$apply(..) . You might be missing the $ in front of scope.. – Foo L Apr 26 '13 at 01:29
  • i can use this: "window.location = login_url" instead of the $location var and that works ... is it okay form in this case? – SeanPlusPlus Apr 26 '13 at 01:40
  • yes you could use window.location, but this would be the quick and dirty way. Try to search the mistake in the code. Is the API Server online? If yes its possible, that you provide a jsFiddle? – tschiela Apr 26 '13 at 05:02
  • i tried it with my factory & I get the same error even before using. Perhaps it's just not valid to inject a $scope into a factory, since there isn't a use case for it. The $scope should be changed in a controller, not in a factory. Looks like this person was able to shoehorn it in by passing the calling scope in: http://stackoverflow.com/a/15298717/1376536 . I would recommend passing in a callback function that will have access to your calling scope. – Foo L Apr 26 '13 at 07:00
  • server not online. i am gonna just go with window.location for now. thanks dudes. – SeanPlusPlus Apr 26 '13 at 21:28

0 Answers0