VLR
VLR

Reputation: 312

How to pass $scope to $factory

Hi Im new in angular js and I want to pass my empty $scope to my $factory for the dynamic login im using the $http to get the data in my API i tried to put the scope in the factory but i didn't work

This is my serviceAPI

(function() {
    "use strict";

    angular.module('starter').factory(serviceAPI', function($http, $q, $ionicLoading, $timeout) {



        function getData() {
            var deferred = $q.defer();
            $ionicLoading.show({ template: 'Loading...' });
            var url = "myAPI";

            var data = {
                username: "admin", <-- this is the one I want to dynamic it
                password: "admin" <--
            };

            $http({
                method: 'POST',
                url: url,
                data: data


            }).success(function(data) {

                $ionicLoading.hide();
                deferred.resolve(data);

            }).error(function() {
                console.log('Error while making HTTP call');
                $ionicLoading.hide();
                deferred.reject();

            });

            return deferred.promise;
        }


        //a Return value to public
        return {
            getData: getData,


        };


    })

}());

this is the controller

(function() {
    "use strict";

    angular.module('starter').controller('LoginCtrl', ['$scope', 'serviceAPI', LoginCtrl]);


    function LoginCtrl($scope, serviceAPI) {
     $scope.username = "";
     $scope.password = "";
        $scope.login = function() {

            serviceAPI.getData().then(function(data) {

                console.log(data)
            });

        }


    }

}());

Upvotes: 0

Views: 199

Answers (1)

Mgnfcnt
Mgnfcnt

Reputation: 129

at service api

function getData(userNameIn, passwordIn) {
//...
var data = {
username: userNameIn,
password: passwordIn
};   //...}

at controller ...

$scope.username = "";
$scope.password = "";
$scope.login = function() {            
serviceAPI.getData($scope.username,$scope.password).then(function(data) {
console.log(data)
});

}

...

Upvotes: 2

Related Questions