0

I created a service called notifier that use toastr to alert the user during the log in process if the user successfully logged in or not.

here is my log in module:

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

    login.controller('mvLoginCtrl', function($scope, $http){
        $scope.signin = function(username, passowrd){
            $http.post('/login', {username:username, passowrd:passowrd}).then(function(response){
                if(response.data.success){
                    mvNotifier.notify('You have successfully signed in!');
                }else{
                    mvNotifier.notify('Username/Password combination incorrect');
                }
            })
        }
    })

i got no errors till here but when i try to sign in i get this obvious error :

ReferenceError: mvNotifier is not defined

i changed my log in module at the line 2 including the required dependencies:

login.controller('mvLoginCtrl', function($scope, $http, mvNotifier) 

but then i got different error

Error: [$injector:unpr] http://errors.angularjs.org/1.2.20/$injector/unpr?p0=mvIdentityProvider%20%3C-%20mvIdentity

and i want to ask what is the reason i got this error and how to solve it. here is my mvNotifier module code:

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

    notifier.value('mvToastr', toastr);
    notifier.factory('mvNotifier', function(myToastr){
        return{
            notify: function(msg){
                mvToastr.success(msg);
                console.log(msg);
            }
        }
    })

thank you.

user3462064
  • 455
  • 2
  • 6
  • 24

2 Answers2

1

You need to inject myNotifier something like this:

var login = angular.module('login',['notifier']);

    login.controller('mvLoginCtrl', ['$scope','$http','mvNotifier',function($scope, $http,mvNotifier){
        $scope.signin = function(username, passowrd){
            $http.post('/login', {username:username, passowrd:passowrd}).then(function(response){
                if(response.data.success){
                    mvNotifier.notify('You have successfully signed in!');
                }else{
                    mvNotifier.notify('Username/Password combination incorrect');
                }
            })
        }
    }])

More on Dependency Injection

V31
  • 7,626
  • 3
  • 26
  • 44
  • Thank you i tired both solutions and still get same error – user3462064 Aug 09 '14 at 03:56
  • Hi I have updated the answer with the correct service name in dependency chain you had named it notifier and I had myNotifier earlier – V31 Aug 09 '14 at 04:00
  • i did noticed this and i write it correctly i even changed your solution the function argument notifier instead of myNotifier but still same error – user3462064 Aug 09 '14 at 04:09
  • I did thank you , but still get the same error here is my app.js maybe it help: var myapp= angular.module('myapp',['ngResource','ngRoute','main','login']); myapp.config(function($routeProvider,$locationProvider){ $locationProvider.html5Mode(true); $routeProvider .when('/',{templateUrl: '/partials/main/main', contorller: 'mvMainCtrl'}) }); – user3462064 Aug 11 '14 at 02:06
  • Hello i just wanna add this comment in case anyone had the same problem, the answer up is correct with couple changes on the services names, and i still have the same problem with the notifier service for a reason i still don't know but it works well with the other services – user3462064 Aug 11 '14 at 04:59
1

You need to add notifier module as a dependency in login module.

var login = angular.module('login',["notifier"]);

Then this should work.

login.controller('mvLoginCtrl', function($scope, $http, mvNotifier) 

Edit: DEMO

Darshan P
  • 2,241
  • 18
  • 16