Javascript 参数';myCtrl';不是函数,未定义
我似乎无法解决这个问题。我有其他控制器以同样的方式工作,但这一个给出了一个错误Javascript 参数';myCtrl';不是函数,未定义,javascript,angularjs,Javascript,Angularjs,我似乎无法解决这个问题。我有其他控制器以同样的方式工作,但这一个给出了一个错误 错误:ng:areq参数错误”“参数'myCtrl'不是函数,未定义。这是我的密码: //js var myApp = angular.module('myApp', ['ngMaterial','angularUtils.directives.dirPagination']); myApp.config(function($interpolateProvider) { $interpo
错误:ng:areq参数错误”“参数'myCtrl'不是函数,未定义
。这是我的密码:
//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>
//js
var myApp=angular.module('myApp',['ngMaterial','angularUtils.directives.dirPagination']);
myApp.config(函数($interpolateProvider){
$interpolateProvider.startSymbol(“”);
});
(功能(){
var myContacts=angular.module('myApp');
myContacts.controller('myCtrl',函数($scope,$http){
$scope.totalContacts=0;
$scope.request_limit=3;//无论API在一个页面上放置多少结果,这都应该匹配
$scope.pagination={
电流:1
};
联络人(1);
//换页
$scope.pageChanged=函数(新页){
获取联系人(新页面);
};
//获取函数
$scope.getContacts=函数(页码){
api_url='/api/people/list?page='+页码;
$http.get(api_url).success(函数(数据){
//更新范围数据
$scope.contacts=data.data;
$scope.totalContacts=data.count
console.log('Data:'+$scope.contacts);
//准备消息输出
如果(data.code==9999){
//显示错误
显示消息('Error',data.msg);
}否则如果(data.code==8888){
//显示错误
显示消息('Error',data.msg);
}else if(data.code==1001){
//没有数据
//显示信息
显示消息('Info',data.msg);
}如果(data.code==1000){
//好的,更新范围
$scope.contacts=data.data;
隐藏信息();
}
});
}
});
})();
//html
...
位置
组织机构
我做错了什么?控制器函数中的这一行可能会抛出一个错误,因为没有定义这样的函数:
getContacts(1)代码>
因此,控制器未正确定义,因此您会得到angular接收到的错误。
尝试删除该行,并将其放在控制器功能的末尾:
$scope.getContacts(1);
作为旁注,您在$scope.pageChanged
函数中也犯了同样的错误。
在那里,您应该将getContacts
替换为$scope.getContacts
。控制器函数中的这一行可能会引发错误,因为未定义此类函数:
getContacts(1)代码>
因此,控制器未正确定义,因此您会得到angular接收到的错误。
尝试删除该行,并将其放在控制器功能的末尾:
$scope.getContacts(1);
作为旁注,您在$scope.pageChanged
函数中也犯了同样的错误。
在这里,您应该用$scope.getContacts
替换getContacts
。您对控制器的定义有点奇怪。
这有点奇怪,因为您的控制器不是应用程序的引导程序
您可以通过以下方式实现控制器:
方法: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
请在这里查一下约翰·帕帕
你对控制器的定义有点奇怪。
这有点奇怪,因为您的控制器不是应用程序的引导程序
您可以通过以下方式实现控制器:
方法: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
请在这里查一下约翰·帕帕
仍然无法理解,为什么要将控制器包含在函数()中,请尝试以下操作:
//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>
//js
var myApp=angular.module('myApp',['ngMaterial','angularUtils.directives.dirPagination']);
myApp.config(函数($interpolateProvider){
$interpolateProvider.startSymbol(“”);
});
控制器('myCtrl',函数($scope,$http){
$scope.totalContacts=0;
$scope.request_limit=3;//无论API在一个页面上放置多少结果,这都应该匹配
$scope.pagination={
电流:1
};
联络人(1);
//换页
$scope.pageChanged=函数(新页){
获取联系人(新页面);
};
//获取函数
$scope.getContacts=函数(页码){
api_url='/api/people/list?page='+页码;
$http.get(api_url).success(函数(数据){
//更新范围数据
$scope.contacts=data.data;
$scope.totalContacts=data.count
console.log('Data:'+$scope.contacts);
//准备消息输出
如果(data.code==9999){
//显示错误
显示消息('Error',data.msg);
}否则如果(data.code==8888){
//显示错误
显示消息('Error',data.msg);
}else if(data.code==1001){
//没有数据
//显示信息
显示消息('Info',data.msg);
}如果(data.code==1000){
//好的,更新范围
$scope.contacts=data.data;
隐藏信息();
}
});
}
});
//html
...