Angularjs离子控制器错误

时间:2016-05-25 19:30:36

标签: angularjs ionic-framework controller ionic-view

我正在使用IONIC Framework(Angularjs)

我收到以下错误,

  463788   error    Error: [ng:areq] http://errors.angularjs.org/1.4.3/ng/areq?p0=PaymentCtrl&p1=not%20a%20function%2C%20got%20undefined
at Error (native)
at http://localhost:8100/lib/ionic/js/angular/angular.min.js:6:416
at Sb (http://localhost:8100/lib/ionic/js/angular/angular.min.js:22:18)
at Qa (http://localhost:8100/lib/ionic/js/angular/angular.min.js:22:105)
at http://localhost:8100/lib/ionic/js/angular/angular.min.js:79:497
at I.appendViewElement (http://localhost:8100/lib/ionic/js/ionic-angular.min.js:17:4463)
at Object.O.render (http://localhost:8100/lib/ionic/js/ionic-angular.min.js:16:17590)
at Object.O.init (http://localhost:8100/lib/ionic/js/ionic-angular.min.js:16:16825)
at I.render (http://localhost:8100/lib/ionic/js/ionic-angular.min.js:17:3419)
at I.register (http://localhost:8100/lib/ionic/js/ionic-angular.min.js:17:3150)

这是我的控制器代码。

define(['ionic', 'ionicAngular', 'angular',
'ngRoute', 'angularAnimate', 'angularSanitize', 'uiRouter'],
    function (ionic, ionicAngular, angular) {

        'use strict';

        console.log('Payment controller ');

        var PaymentCtrl = function ($scope, PaymentSvc,$state, $ionicLoading) {
          /*$scope.phoneNumberVerification = function() { $state,$ionicPopup,
          console.log('PhoneNumber controller added1 ');
          $ionicLoading.hide();
          $state.go('tab.eateries');
        };*/


                      // When button is clicked, the popup will be shown...

        };
        return PaymentCtrl;

    });

Serveics.js

define(['ionic', 'ionicAngular', 'angular',
    'ngRoute', 'angularAnimate', 'angularSanitize', 'uiRouter'],
        function (ionic, ionicAngular, angular) {

            'use strict';
              //console.log('service modules');
             var PaymentSvc = function(){

                     console.log('serverices call');//var svc = this;

            }
            return PaymentSvc;

        });



       // });*/

payment.js

define(['ionic', 'ionicAngular', 'angular',
    './modules/payment/controllers/paymentctrl',
    './modules/payment/services/services',
    'ngRoute', 'angularAnimate', 'angularSanitize', 'uiRouter'],
        function (ionic, ionicAngular, angular,
                    paymentCtrl,
                    paymentSvc) {

            'use strict';

            console.log('payment.js modules');

            var payment = angular.module('payment', ['ionic'])
                    .controller('PaymentCtrl', paymentCtrl)
                    .service('PaymentSvc',paymentSvc);
            return payment;
        });

2 个答案:

答案 0 :(得分:1)

这是注射错误。例如,如果你注入[' a',' b' c']你必须在你的函数中以相同的顺序和数量:函数(函数) A,b,C)。在您的情况下,注入中的参数比控制器函数中的参数多。

答案 1 :(得分:1)

无需注入['angular','ngRoute', 'angularAnimate', 'angularSanitize', 'uiRouter']。当您注入['ionic']

时,I​​onic会自动注入角度

直接编写控制器

angular.module('starter', ['ionic']).controller('PayCtrl',function ($scope,$state,$ionicLoading,PaymentSvc){

//starter is the app name come from ng-app="starter" 

         $ionicLoading.show();
         $scope.phoneNumberVerification = function(){
              console.log('PhoneNumber controller added1');
              $ionicLoading.hide();
              $state.go('tab.eateries');
         };
    });

我建议您将javascript项目文件整理到3个文件中:

app.js ,其中包含

angular.module('starter', ['ionic', 'starter.controllers','starter.services'])..config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'AppCtrl'
  }).state('app.home', {
    url: '/home',
    views: {
      'tab-home': {
        templateUrl: 'templates/home.html',
        controller : 'HomeCtrl'
      }
    }
  });
  $urlRouterProvider.otherwise('/app/home');
});

controller.js ,其中包含您的控制器

angular.module('starter.controllers', []).controller('AppCtrl', function('PayCtrl',function ($scope,$state,$ionicLoading,PaymentSvc){
         $ionicLoading.show();
         $scope.phoneNumberVerification = function(){
              console.log('PhoneNumber controller added1');
              $ionicLoading.hide();
              $state.go('tab.eateries');
         };
    });

service.js ,其中包含您与服务器的连接

angular.module('starter.services', []).factory('PaymentSvc',function($http,$q){

});