【发布时间】:2014-09-15 17:56:57
【问题描述】:
我是 Angularjs 的新手,我正在学习一个教程,但我得到了标题中的错误。
HTML 代码:
<div data-ng-app="myApp">
<div data-ng-controller="MyCtrl">
<form>
<table>
<tr style="font-weight: bold">
<td>ID</td>
<td>Name</td>
<td>Surname</td>
<td>House</td>
<td>Address</td>
<td>Locality</td>
<td>Contact</td>
<td>Contact 2</td>
<td>Contact 3</td>
<td>Reply</td>
<td>Edit</td>
</tr>
<tr data-ng-repeat="person in persons">
<td>{{person.ID}}</td>
<td>{{person.Name}}</td>
<td>{{person.Surname}}</td>
<td>{{person.House}}</td>
<td>{{person.Address}}</td>
</tr>
</table>
</form>
</div>
</div>
<script type="text/javascript">
//Defining a Angular module
var myApp = angular.module('myApp', []);
//Defining a Angular Controller
myApp.controller('MyCtrl', ['$scope', '$http', function ($scope, $http) {
//Retrieving the List of people
GetPersons();
//Displaying the Save button
$scope.DisplaySave = true;
function GetPersons() {
//Defining the $http service for getting the people
$http({
method: 'GET', url: '/api/data'
}).
success(function (data) {
if (data != null || data != 'undefined') {
//Assigning people data to the $scope variable
$scope.persons = data;
//Clearing the Person object in create context and Showing default Gender(Male) Checked
$scope.newperson = {
Id: ''
};
}
})
.error(function (error) {
//Showing error message
$scope.status = 'Unable to retrieve people' + error.message;
});
}
} ]);
</script>
API 控制器:
public class DataController : ApiController
{
//GET api/data
public IEnumerable<CommonLayer.Telesales> GetPeople()
{
return new BusinessLayer.Telesales().getUserSession(User.Identity.Name).AsEnumerable();
}
更多错误详情:
magicplayer:init: set version: 1.0.1
adme: onDOMStart: got code: user_key=f52009a2292c2b524ac9af2801caef4c443d7cdc7697dff171f77b3c81cd26fa gender=1 age=4
Error: [ng:areq] Argument 'MyCtrl' is not a function, got undefined
http://errors.angularjs.org/1.2.18/ng/areq?p0=MyCtrl&p1=not%20a%20function%2C%20got%20undefined
at http://localhost:12570/Scripts/angular.js:78:12
at assertArg (http://localhost:12570/Scripts/angular.js:1475:11)
at assertArgFn (http://localhost:12570/Scripts/angular.js:1485:3)
at http://localhost:12570/Scripts/angular.js:7198:9
at http://localhost:12570/Scripts/angular.js:6592:34
at forEach (http://localhost:12570/Scripts/angular.js:327:20)
at nodeLinkFn (http://localhost:12570/Scripts/angular.js:6579:11)
at compositeLinkFn (http://localhost:12570/Scripts/angular.js:6028:13)
at compositeLinkFn (http://localhost:12570/Scripts/angular.js:6031:13)
at compositeLinkFn (http://localhost:12570/Scripts/angular.js:6031:13)
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:12570/Images/accent.png
onMessageFromBackground: method=statPixel
我做错了什么?我搜索其他解决方案,但似乎我的问题有点不同。
【问题讨论】:
-
你的控制器在哪里命名为“MyCtrl”???
-
是的............
-
@KartikeyaKhosla 我尝试将名称从 MyCtrl 更改为 Data 但仍然是错误
-
不完全确定我改变了什么(除了删除循环中人员的“s”),但它似乎正在工作:JSBin
-
@Goodzilla 是的,我忘记删除我对我所做的“s”,但我仍然得到同样的错误。错误发生在 myApp.controller( 行,因为 GetPersons() 甚至没有被调用
标签: javascript asp.net-mvc angularjs