signuppanel.js

angular.module('homepage').
directive('signuppanel', function() {
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        templateUrl: 'components/content-components/login/signuppanel.html',
        controller: 'signupPanelController'
    };
}).
controller('signupPanelController', function($scope, $timeout, $window, Authentication, Server) {
    $scope.isMessageVisible = false;
	$scope.error = {
		email: false,
		username: false
	};

    $scope.registerUser = function() {
        var userData = $scope.registrationInfo;
        console.log(userData);

        if(isValidData(userData)) {
            Server.signup(userData).
            success(function(data) {
                console.log(data);
                $scope.message = data.message;
                if(data.success) {
                    $scope.isMessageVisible = true;
                    $scope.registrationInfo =  {};
                    $timeout(function () {
                        Authentication.setUser(data.token);
                        $window.location.href = '/#/home';
                    }, 3000);
                }
                else {
                    $scope.isMessageVisible = true;
                    $timeout(function () {
                        $scope.isMessageVisible = false;
                    }, 3000);
                }
            });
        }
    };

    function isValidData(data) {
        if(!data) {
            return false;
        }
        var count = 0;
        for(var key in data) {
            if(data.hasOwnProperty(key)) {
                count++;
            }
        }
        // Number of mandatory fields
        if(count === 5) {
            return true;
        }
    }
})