0
var app = angular.module('part3', []);
app.controller('Part3Controller' ,function ($scope) {
    $scope.Message1 = "apek";
    $scope.IsLogedIn = false;
    $scope.Message = '';
    $scope.Submitted = false;
    $scope.IsFormValid = false;
    $scope.LoginData = {

        UserName: '',
        Password: ''
    };
    $scope.$watch('f1.$valid', function (newVal) {
        $scope.IsFormValid = newVal;
    });

    $scope.Login = function () {

        $scope.Submitted = true;

        if ($scope.IsFormValid) {
            alert("eeeeee")
            alert("dsds")
            serv.GetUser($scope.LoginData).then(function (d) {
                alert("dsdsdfsdfs");
                if (d.data.UserName != null) {
                    alert("he;llo1");
                    $scope.IsLoggedIn = true;
                    $scope.Message = "Successfully Login" + d.data.FullName;
                }
                else {

                    alert("Invalid Credential")
                }
            })
        }
    }



})

app.factory('serv', function ($http) {
    alert("helloo");
    var fac = {};
    fac.GetUser = function (d) {
        $scope.Submitted = true;
        return $http({

            url: '/Data/UserLogin',
            method: 'POST',
            data: JSON.stringify(d),
            header: { 'content-type': 'application/json' }
        });
    };
    return fac;
});

I had written above code for login functionality in my app but it is not calling factory "serv" its showing error "angular.min.js:92 ReferenceError: serv is not defined"

"angular.min.js:92 ReferenceError: serv is not defined"

2 Answers 2

4

When you want to use a factory or service with a controller in angular, you need to inject them with that. We have:
app.factory('name_of_factory', factory_function);
and controller:
app.controller('name_of_controller', controller_function($scope,factory_name_you_want_to_use) { //blah blah });
That was some of explanation, you just need to inject name of factory with your controller. Your Controller JS will look like this:

app.controller('Part3Controller' ,function ($scope,serv){//yourcode});
Sign up to request clarification or add additional context in comments.

Comments

0

need to inject serv factory to the controller

controller('Part3Controller' ,function ($scope,serv) {

3 Comments

still it is not executing " url: '/Data/UserLogin'," Userlogin function in DataController in mvc
.factory('LoginService', function ($http) { alert("helloo"); var fac = {}; fac.GetUser = function (d,e) { $scope.Submitted = true; return $http({ url: '/Data/UserLogin', method: 'POST', data: JSON.stringify(d), header: { 'content-type': 'application/json' } }); alert(4) }; return fac; });
remove $scope.Submitted = true; in the factory. factory does not support scope variables

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.