k.vincent
k.vincent

Reputation: 4133

How to pass $scope to factory

I'm trying to pass a scope to a factory and use it in the controller where the function should be invoked, but it just doesn't work.

var myApp = angular.module('mainApp', []);

myApp.factory("UserService", function ($http) {
var users = ["xxxxx", "yyyyy", "zzzzz"];

return {
    all: function () {
        return users;
        console.log( users );
    },
    first: function () {
        return users[1];
    },
    checkUser: function(elUrl, classHide, classShow){
        $http.post(elUrl)
        .success(function (data, status, headers, config) {
            if (!data.isMember) {
                el = classHide //should be when invoking: $scope.mailexist = classHide;
                $('form').submit();
            } else {
                el = classShow //should be when invoking: $scope.mailexist = classShow;
                return false;
            }
        }).error(function (data, status, header, config) {
            //$scope.ResponseDetails = "Data: " + data +
            "<hr />status: " + status +
            "<hr />headers: " + header +
            "<hr />config: " + config;
        });
    }
};
});
 myApp.controller('mainCtrl', ['$scope', '$http', 'UserService', function ($scope, $http, UserService) {

UserService.checkUser( 'inc/memberSearch.jsp?user.Email=' + $scope.umail + '&group.PK=' + angular.element('.grp').val(),
            $scope.mailexist,
            'mail-exist-hide',
            'mail-exist-show');

}]);

mailexists is the ng-class for the text element in the HTML which should be shown or hidden depending on the server response:

<p ng-class="mailexist">Text Text text Text</p>

The function in the controller is being invoked but The Text doesn't appear. If I add the $scope in the factory, then I get $scope is not defined which is true. But $scope can't be injected in a factory/service.

Any ideas?

Upvotes: 1

Views: 1702

Answers (3)

Hmahwish
Hmahwish

Reputation: 2232

You should handle the success or error in the controller , just return the response

checkUser: function(elUrl, classHide, classShow){
  return  $http.post(elUrl)
}

handle in the controller

UserService.checkUser( 'inc/memberSearch.jsp?user.Email=' + $scope.umail + '&group.PK=' + angular.element('.grp').val(),
        $scope.mailexist,
        'mail-exist-hide',
        'mail-exist-show').then(function(){
            if (!response.data.isMember) {
        $scope.mailexits = 'mail-exist-hide'; 
        $('form').submit();
    } else {
        $scope.mailexits = 'mail-exist-show'; 
        return false;
    }
        }, function(response){
        $scope.ResponseDetails = "Data: " + response.data;
        });}]);

Upvotes: 2

pwolaq
pwolaq

Reputation: 6381

factory in MVC represents model and shouldn't be coupled with view - thing you are trying to achieve should be handled by controller

something like this should be ok:

var myApp = angular.module('mainApp', []);

myApp.factory("UserService", function ($http) {
    var users = ["xxxxx", "yyyyy", "zzzzz"];

    return {
        all: function () {
            return users;
            console.log( users );
        },
        first: function () {
            return users[1];
        },
        checkUser: function(elUrl, classHide, classShow){
            return $http.post(elUrl);
        }
    };
});

myApp.controller('mainCtrl', ['$scope', '$http', 'UserService', function ($scope, $http, UserService) {

UserService.checkUser( 'inc/memberSearch.jsp?user.Email=' + $scope.umail + '&group.PK=' + angular.element('.grp').val(),
            $scope.mailexist,
            'mail-exist-hide',
            'mail-exist-show').then(function(){
                // on success
            }, function(){
                // on error
            });
}]);

Upvotes: 1

Ujjwal kaushik
Ujjwal kaushik

Reputation: 1686

Send $scope as parameter to factory like

UserService.checkUser($scope,param1,..);

Than use this in factory

checkUser: function(scope,p1,...){

now use this scope in factory :) hope this will help

Upvotes: 0

Related Questions