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
8
  • 1
    Do you need a $ in front of scope.$apply(...) ? So $scope.$apply... Commented Apr 26, 2013 at 0:55
  • if i set the call to scope.apply i still get the err: Error: Unknown provider: $scopeProvider <- $scope <- user <- navigationDirective Commented Apr 26, 2013 at 1:13
  • if i don't pass the $scope the function, i get this err: ReferenceError: scope is not defined Commented Apr 26, 2013 at 1:14
  • 1
    I meant for you to try $scope.$apply(..) . You might be missing the $ in front of scope.. Commented Apr 26, 2013 at 1:29
  • i can use this: "window.location = login_url" instead of the $location var and that works ... is it okay form in this case? Commented Apr 26, 2013 at 1:40

0

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.