在控制器中调用AngularJS服务函数时出错?

时间:2016-11-28 11:44:33

标签: javascript angularjs angular-services

我写了一个服务,它从一个控制器获取数据并将其发送到另一个控制器。而这个控制器指向不同的模块。现在,当我从控制器调用服务函数时,它会触发一些错误。

  

angular.js:13920 ReferenceError:未定义addData       在Object.dataservice

以下是 pages.module.js

中的服务
['/whatever/1', '/whatever/2', '/whatever/3'].forEach(function(value){
 if (response.config.url.startsWith(value)) {
   // do something
 }
})


return response;

这是第一个控制器 login.controller.js

(function ()
{
    'use strict';

    angular
        .module('app.pages', [
            'app.pages.auth.login',
            'app.pages.auth.login-v2',
            'app.pages.auth.register',
            'app.pages.auth.register-v2',
            'app.pages.auth.verify-mobile',
            'app.pages.auth.reset-password',
            'app.pages.auth.lock',
            'app.pages.coming-soon',
            'app.pages.error-404',
            'app.pages.error-500',
            'app.pages.invoice',
            'app.pages.maintenance',
            'app.pages.profile',
            'app.pages.search',
            'app.pages.timeline'
        ])
        .config(config)
        .factory('dataservice', dataservice);

    /** @ngInject */
    function config(msNavigationServiceProvider)
    {
        // Navigation
        msNavigationServiceProvider.saveItem('pages', {
            title : 'PAGES',
            group : true,
            weight: 2
        });
    }

     function dataservice(){

        var sendarr = [];

        this.addData = function(newObj) {
            sendarr.push(newObj);
        };

        this.getData = function(){
            return sendarr;
        };

        return {
            addData: addData,
            getData: getData
        };

    }
})();

这是第二个控制器 verify-mobile.controller.js

(function ()
{
    'use strict';

    angular
        .module('app.pages.auth.login')
        .controller('LoginController', LoginController);

    /** @ngInject */
    LoginController.$inject = ['dataservice'];
    function LoginController(msApi,$state,dataservice)
    {
        // Data
        var vm = this;

        vm.login = login;
        vm.startApp = startApp;
        vm.fbLogin = fbLogin;
        var auth2;
        // Methods
        function fbLogin(){
            FB.login(function(response){
                if(response.status=='connected'){
                    testAPI();
                }
                else if(response.status == 'not_authorized'){
                    console.log('error');
                }
                else{
                    console.log('please log in');
                }
            });
        }

        function testAPI() {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', function(response) {
                console.log('Successful login for: ' + response.name);

            });
        }

        function startApp(){
            gapi.load('auth2', function(){
            // Retrieve the singleton for the GoogleAuth library and set up the client.
                auth2 = gapi.auth2.init({
                client_id: '990822731291-21sdd22ujqc78l1q2i2lmf5hfe5satj1.apps.googleusercontent.com',
                cookiepolicy: 'single_host_origin',
                fetch_basic_profile: 'true',
                // Request scopes in addition to 'profile' and 'email'
                //scope: 'additional_scope'
                });
                attachSignin(document.getElementById('customGoogleBtn'));
             });
        }

        function attachSignin(element) {
            auth2.attachClickHandler(element, {},
                function(googleUser) {
                    var profile = googleUser.getBasicProfile();
                    console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
                    console.log('Name: ' + profile.getName());
                    console.log('Image URL: ' + profile.getImageUrl());
                    console.log('Email: ' + profile.getEmail());
                    var pushData = [profile.getId(), profile.getName(), profile.getEmail()];
                    console.log(pushData);
                    dataservice.addData(pushData);
                    $state.go('app.pages_auth_verify-mobile')
                }, 
                function(error) {
                    alert(JSON.stringify(error, undefined, 2));
            });
        }

        function login(){
            var jsonData = {"mobile":vm.form.mobile};
            msApi.request('login.credentials@save',jsonData,
                // SUCCESS
                function (response)
                {
                   console.log(response.error);
                    if(response.error == 1){
                        vm.form.mobileErrorFlag = true;
                    }
                    if(response.error == 0){
                        vm.form.mobileErrorFlag = false;
                    }
                },
                // ERROR
                function (response)
                {
                    alert(JSON.stringify(response));
                }
            )
        }



    }
})();

1 个答案:

答案 0 :(得分:1)

您的<compatible-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="false" android:xlargeScreens="false" /> 中的方法为this.addDatathis.getData,而您在返回时没有使用dataservice访问该方法。这就是你得到这个错误的原因。

您不需要this内的this,也可以删除以下内容。

factory service
相关问题