AngularJS - 服务未在函数中定义错误但在控制器中没有问题 - ReferenceError:未定义systemService

时间:2016-11-24 12:05:10

标签: javascript angularjs

我对AngularJS很新,所以这可能非常明显!

我有一项服务可以进行一些基本检查,然后返回信息。我在控制器和函数中调用此服务。

当我在控制器中调用它时,它可以正常工作而不会出错。

当我在函数中调用它时

  

angular.js:9101 ReferenceError:未定义systemService

调用有效的控制器中的服务:

myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
            $scope.ContinueAngularMethod = function () {

                $rootScope.field = 'payment';
                $scope.field = $rootScope.field;
                $scope.notApp = '1';
                console.log("I don't error in here");
                $scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
                $rootScope.$apply();
            }
        }]);

在无效的功能中呼叫服务:

function MyCtrl($scope) {

                $scope.changeme = function () {
                    console.log("Drop down changes ideally do something here....");

                    //Call to service  
                    $scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);

                    console.log($scope.ss.field);
                    console.log($scope.ss.notAppJ);

                }

这是我的完整代码:

<script type='text/javascript'>

        //Angular stuff
        var myApp = angular.module('myApp', []);

        //Set up my service
        myApp.service('systemService', function () {

            this.info = {}; //Declaring the object

            this.checkDropDownValue = function (type, notAppJ) {


                if (type != 'PayPal') {
                    console.log("I am not PayPal");
                    field = 'other'
                    notApp = '0';
                }
                else if (type == 'PayPal' && notApp == '1') {
                    console.log("i am in the else if - functionality later");
                }
                else {
                    console.log("i am in the else - functionality later");
                }

                this.info.field = type;
                this.info.number = notApp;

                return this.info;
            };

        });

        myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
            $scope.ContinueAngularMethod = function () {

                $rootScope.field = 'payment';
                $scope.field = $rootScope.field;
                $scope.notApp = '1';
                console.log("I don't error in here");
                $scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
                $rootScope.$apply();
            }
        }]);

        function MyCtrl($scope) {

            $scope.changeme = function () {
                console.log("Drop down changes ideally do something here....");

                //Call to service  
                $scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);

                console.log($scope.ss.field);
                console.log($scope.ss.notAppJ);

            }

            $scope.myFunct = function (keyEvent) {

            if (keyEvent.which === 13) {
                //They hit the enter key so ignore this 
                keyEvent.preventDefault();
            }

            //Becauase a new child scope is generated you can't use $scope as that refers to the parent . But this refers to the child. 
            var rPromise = findAll(this.softwareText);

            }
        }

    </script>

我没试过就试过这些:

angular Uncaught ReferenceError: Service is not defined

AngularJS - ReferenceError: $ is not defined

Initialize $scope variables for multiple controllers - AngularJS

2 个答案:

答案 0 :(得分:4)

不推荐使用定义MyCtrl,您需要使用angular来注入systemService依赖项才能使用它。

尝试按照定义continueController的方式定义MyCtrl控制器,如下所示:

    myApp.controller('MyCtrl', ["$scope", 'systemService', function ($scope, systemService) {
        $scope.changeme = function () {
            console.log("Drop down changes ideally do something here....");

            //Call to service  
            $scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);

            console.log($scope.ss.field);
            console.log($scope.ss.notAppJ);

        }

        $scope.myFunct = function (keyEvent) {

        if (keyEvent.which === 13) {
            //They hit the enter key so ignore this 
            keyEvent.preventDefault();
        }

        //Becauase a new child scope is generated you can't use $scope as that refers to the parent . But this refers to the child. 
        var rPromise = findAll(this.softwareText);

        }
    }]);

答案 1 :(得分:1)

MyCtrl shoud通过$inject property提供依赖systemService

function MyCtrl($scope, systemService) {

}

MyCtrl.$inject = ['$scope', 'systemService']