javascript - AngularJS - 服务未在函数中定义错误,但在 Controller 中正常 - ReferenceError : systemService is not defined

标签 javascript angularjs

我对 AngularJS 还很陌生,所以这可能是非常明显的!

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

当我在 Controller 中调用它时,它工作正常,没有错误。

当我在函数中调用它时,我得到

angular.js:9101 ReferenceError: systemService is not defined

调用 Controller 中有效的服务:

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

最佳答案

不推荐使用定义 MyCtrl 的方式,您需要 Angular 注入(inject) systemService 依赖项才能使用它。

尝试以定义 continueController 的方式定义 MyCtrl Controller ,如下所示:

    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);

        }
    }]);

关于javascript - AngularJS - 服务未在函数中定义错误,但在 Controller 中正常 - ReferenceError : systemService is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785984/

相关文章:

javascript - $broadcast 的问题

javascript - AngularJS 从 MongoDB GridFS 播放视频

javascript - 如何在angularjs中使用拆分

Javascript 对象括号表示法和 requirejs

angularjs - 当所有值都等于零时,Nvd3 Multibarchart forceY 选项不起作用

javascript - 以 Angular 获取元素的绑定(bind)值

javascript - 为什么我的指令在使用 AngularJS 1.5 时第一次使用 ng-if 运行时没有进入动画?

javascript - 流类型 'keydown' 事件

javascript - jQuery 委托(delegate)事件绑定(bind)不起作用?

javascript - 从浏览器调用电话时,如何以编程方式知道电话何时被接听?