当我将控制器代码包含在$(function () {});
方法中时,它停止工作。请找到下面提到的示例代码:
联系人.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Contacts</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="ContactsController">
<form>
<label>Name</label>
<input type="text" name="name" ng-model="newcontact.name" />
<label>Email</label>
<input type="text" name="email" ng-model="newcontact.email" />
<label>Phone</label>
<input type="text" name="phone" ng-model="newcontact.phone" />
<br />
<input type="hidden" ng-model="newcontact.id" />
<input type="button" value="Save" ng-click="saveContact()" class="btn btn-primary" />
</form>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts">
<td>{{contact.name}}</td>
<td>{{contact.email}}</td>
<td>{{contact.phone}}</td>
<td>
<a href="#" ng-click="edit(contact.id)">edit</a>
<a href="#" ng-click="delete(contact.id)">delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<script type="text/javascript">
var uid = 1;
$(function () {
function ContactsController($scope) {
$scope.contacts = [
{ id: 0, 'name': 'Viral', 'email': 'hello@gmail.com', 'phone': '123-2343-44' }
];
}
});
</script>
如果我删除该$(function () {});
方法,那么它开始工作并提供所需的输出。任何人都可以帮助我了解有关该问题的详细信息。