如何将我的服务注入我的控制器角度js

时间:2014-04-19 04:21:35

标签: javascript angularjs dependency-injection angularjs-service

我对angular JS比较新,我对angularJS 1.3.0 beta版本有一个问题

我正在尝试将我的服务(独立模块)插入控制器。

这是我的应用代码

'use strict';
angular.module('lifecareApp', [
   'lifecareApp.validationServices'
   , 'lifecareApp.loginController'
   , 'lifecareApp.signupController'
]).
config(function ($routeProvider, $locationProvider) {
  $routeProvider.
    when('/account/login', {
      controller: 'loginController'
    }).
    when('/account/signup', {
      controller: 'signupController'
    })
  $locationProvider.html5Mode(true);
});

这是我的服务代码

'use strict';

angular.module('lifecareApp.validationServices', []).
    factory('validationServices', function () {
    return {
        validateRequiredField: function (value, requiredMessage) {
            if (value){
                return false; //returns false
            }else{
                if (requiredMessage){
                    return requiredMessage;
                }else{
                    return "Required";
                }
            }
        },
        validateEmail: function (value, required, requiredMessage, invalidEmailMessage){
            //validate if its required first
            if (required){
                if (value){
                    //validate the email next
                    var checkEmailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                    if (checkEmailRegex.test(value)){
                        return false;
                    }else{
                        if (invalidEmailMessage){
                            return false;
                        }else{
                            return "Invalid Email";
                        }
                    }
                }else{
                    if (requiredMessage){
                        return requiredMessage;
                    }else{
                        return "Required";
                    }
                }
            }
        }
    };

});

这是我的控制器代码

'use strict';

/* Controllers */

angular.module('lifecareApp.loginController', []).
  controller('loginController', ['$scope', 'validationServices' function ($scope, validationServices) {
        $scope.emailError = false;
        $scope.passwordError = false;
        $scope.overallError = false;

        $scope.login = function(){
            var email = $scope.tbEmail;
            var password = $scope.tbPassword;

            var passwordValidation = validationServices.validateRequiredField(password);
            var emailValidation = validationServices.validateEmail(email, true);

            if (emailValidation){
                $scope.emailError = true;
                $scope.valEmail = emailValidation;
            }else{
                $scope.valEmail = "";
                $scope.emailError = false;
            }

            if (passwordValidation){
                $scope.passwordError = true;
                $scope.valPassword = passwordValidation;
            }else{
                $scope.valPassword = "";
                $scope.passwordError = false;
            }


            if (passwordValidation || emailValidation){
                $scope.overallError = true;
                $scope.valError = "Login Error!";
                return;
            }else{
                $scope.overallError = true;
                $scope.valError = "";
            }
        };
  }]);

我一直收到这个错误。 未捕获的错误:[$ injector:modulerr] http://errors.angularjs.org/1.3.0-beta.5/ $ injector / modulerr?p0 = lifecareApp&amp; ... ngularjs.org%2F1.3.0-beta.5%2F%24injector%2Funpr%3Fp0%3D%2524routeProvider ... ... 5)

请帮忙! =(

我还发现,角度1.0.7没有此错误,而最新角度1.2.16和1.3.0有此错误。

1 个答案:

答案 0 :(得分:0)

在主模块中,您需要在新版本的angularjs

中包含ngRoute依赖项

angular.module('lifecareApp',['ngRoute',....

还要记住包含路线脚本文件

<script src="angular-route.js">