参数'myCtrl'不是函数,未定义

时间:2016-03-09 09:13:55

标签: javascript angularjs

我似乎无法解决这个问题。我有其他控制器以相同的方式完成工作正常但这一个给出了一个错误 Error: ng:areq Bad Argument" "Argument 'myCtrl' is not a function, got undefined。这是我的代码:

//js
var myApp = angular.module('myApp', ['ngMaterial','angularUtils.directives.dirPagination']);

myApp.config(function($interpolateProvider) {
              $interpolateProvider.startSymbol('<@');
              $interpolateProvider.endSymbol('@>');
        });

(function(){

     var myContacts = angular.module('myApp');

     myContacts.controller('myCtrl', function ($scope, $http) {


            $scope.totalContacts = 0;
            $scope.request_limit = 3; // this should match however many results your API puts on one page

            $scope.pagination = {
                current: 1
            };

            getContacts(1); 

            // Page changed
            $scope.pageChanged = function(newPage) {

                getContacts(newPage);
            };


            // Get function 
            $scope.getContacts = function(pageNumber){

                api_url = '/api/people/list?page=' + pageNumber;


                $http.get(api_url).success(function(data){


                    // Update the scope data
                    $scope.contacts = data.data;
                    $scope.totalContacts = data.count

                    console.log('Data: '+ $scope.contacts);

                    // Prepare message output
                    if(data.code == 9999) {

                        // Show error
                        displayMessage('Error', data.msg);

                    } else if (data.code == 8888) {

                        // Show error
                        displayMessage('Error', data.msg);

                    } else if (data.code == 1001) {

                        // No data
                        // Show info
                        displayMessage('Info', data.msg);

                    } else if (data.code == 1000) { 

                        // OK, update the scope
                        $scope.contacts = data.data;
                        hideMessage();
                    }
                });
            }   
        });
})();

// html
<html lang="en" ng-app="myApp">
<head>...
 <div data-ng-controller="myCtrl"  class="container-fluid" id="pcont">
  <table class="table no-border hover">
   <thead class="no-border">
    <tr>
     <th class="text-center"><strong>Position</strong></th>
     <th class="text-center"><strong>Organization</strong></th>
    </tr>
   </thead>
   <tbody class="no-border-y">
    <tr dir-paginate="contact in contacts | itemsPerPage: request_limit" total-items="totalContacts"  current-page="pagination.current">
     <td class="text-center"><@ contact.contact_position @></td>
     <td class="text-center"><@ contact.organization_name @></td>
    </tr>
   </tbody> 
  </table> 

我在那里做错了什么?

4 个答案:

答案 0 :(得分:6)

控制器函数中的这一行可能会抛出错误,因为没有定义这样的函数:

getContacts(1);

因此,控制器未正确定义,因此您会收到angular收到的错误。 尝试删除该行,而不是将其放在控制器功能的末尾:

$scope.getContacts(1);

作为旁注,$scope.pageChanged函数中存在同样的错误。 在那里,您也应该getContacts替换$scope.getContacts

答案 1 :(得分:0)

你对控制器的定义有点怪异。 它有点奇怪,因为你的控制器不是你的应用程序的引导程序。

您可以通过以下方式实施控制器:

方式:1

          var myApp = angular.module('myApp', ['ngMaterial','angularUtils.directives.dirPagination']);

        myApp.config(function($interpolateProvider) {
              $interpolateProvider.startSymbol('<@');
              $interpolateProvider.endSymbol('@>');

        });
       myApp.controller('myCtrl', function ($scope, $http) {
         // .. Rest of the controller code

查看本指南: Angular controller guide

方法:2。

var myApp = angular.module('myApp',['ngMaterial','angularUtils.directives.dirPagination'])
.controller('myCtrl', myCtrl)

function myCtrl($location, common , config) {

}

请在这里查看John papa angular convention John Papa Convention

答案 2 :(得分:0)

仍然没有得到,为什么你把控制器包含在一个函数()中,试试这个:

//js
var myApp = angular.module('myApp', ['ngMaterial','angularUtils.directives.dirPagination']);

myApp.config(function($interpolateProvider) {
              $interpolateProvider.startSymbol('<@');
              $interpolateProvider.endSymbol('@>');
        });

myApp.controller('myCtrl', function ($scope, $http) {


            $scope.totalContacts = 0;
            $scope.request_limit = 3; // this should match however many results your API puts on one page

            $scope.pagination = {
                current: 1
            };

            getContacts(1); 

            // Page changed
            $scope.pageChanged = function(newPage) {

                getContacts(newPage);
            };


            // Get function 
            $scope.getContacts = function(pageNumber){

                api_url = '/api/people/list?page=' + pageNumber;


                $http.get(api_url).success(function(data){


                    // Update the scope data
                    $scope.contacts = data.data;
                    $scope.totalContacts = data.count

                    console.log('Data: '+ $scope.contacts);

                    // Prepare message output
                    if(data.code == 9999) {

                        // Show error
                        displayMessage('Error', data.msg);

                    } else if (data.code == 8888) {

                        // Show error
                        displayMessage('Error', data.msg);

                    } else if (data.code == 1001) {

                        // No data
                        // Show info
                        displayMessage('Info', data.msg);

                    } else if (data.code == 1000) { 

                        // OK, update the scope
                        $scope.contacts = data.data;
                        hideMessage();
                    }
                });
            }   
        });

// html
<html lang="en" ng-app="myApp">
<head>...
 <div data-ng-controller="myCtrl"  class="container-fluid" id="pcont">
  <table class="table no-border hover">
   <thead class="no-border">
    <tr>
     <th class="text-center"><strong>Position</strong></th>
     <th class="text-center"><strong>Organization</strong></th>
    </tr>
   </thead>
   <tbody class="no-border-y">
    <tr dir-paginate="contact in contacts | itemsPerPage: request_limit" total-items="totalContacts"  current-page="pagination.current">
     <td class="text-center"><@ contact.contact_position @></td>
     <td class="text-center"><@ contact.organization_name @></td>
    </tr>
   </tbody> 
  </table> 

答案 3 :(得分:0)

我似乎无法在您的代码中找到错误,因为它是非常难的格式。我只是试一试,试试吧。

(function () {
    var myApp = angular.module('myApp', ['ngMaterial', 'angularUtils.directives.dirPagination']);

    myApp.config(function ($interpolateProvider) {
        $interpolateProvider.startSymbol('<@');
        $interpolateProvider.endSymbol('@>');
    });

    var myContacts = angular.module('myApp');

    myContacts.controller('myCtrl', function ($scope, $http) {


        $scope.totalContacts = 0;
        $scope.request_limit = 3; // this should match however many results your API puts on one page

        $scope.pagination = {
            current: 1
        };

        $scope.getContacts(1);

        // Page changed
        $scope.pageChanged = function (newPage) {

             $scope.getContacts(newPage);
        };


        // Get function 
        $scope.getContacts = function (pageNumber) {

            api_url = '/api/people/list?page=' + pageNumber;


            $http.get(api_url).success(function (data) {


                // Update the scope data
                $scope.contacts = data.data;
                $scope.totalContacts = data.count

                console.log('Data: ' + $scope.contacts);

                // Prepare message output
                if (data.code == 9999) {

                    // Show error
                    displayMessage('Error', data.msg);

                } else if (data.code == 8888) {

                    // Show error
                    displayMessage('Error', data.msg);

                } else if (data.code == 1001) {

                    // No data
                    // Show info
                    displayMessage('Info', data.msg);

                } else if (data.code == 1000) {

                    // OK, update the scope
                    $scope.contacts = data.data;
                    hideMessage();
                }
            });
        }
    });
})();// JavaScript source code