我花了几天时间查看其他答案,因为ng-click没有在这里调用控制器功能,我仍然无法弄清楚我做错了什么。
我有一个主脚本.js,其中定义了控制器:
var dbApp = angular.module('dbApp', ['ngRoute','ngResource']);
dbApp.config(function($routeProvider) {
$routeProvider
.when('/managedevices', {
templateUrl : 'pages/managedevices.html',
controller : 'manageDevicesController'
})
});
dbApp.controller('manageDevicesController', function($scope, $http) {
$scope.message = 'manageDevicesController, you say?';
var dataForThisTest = "json=" + encodeURI(JSON.stringify([
{
id: 1,
firstName: "Test",
lastName: "Person"},
{
id: 2,
firstName: "Test2",
lastName: "Person2"}
]));
$scope.people = [];
$scope.loadPeople = function() {
var httpRequest = $http({
method: 'POST',
url: '/echo/json/',
data: dataForThisTest
}).success(function(data, status) {
$scope.people = data;
});
};
});
然后我有一个HTML索引文件,它可以完美地显示部分文件。
部分看起来像:
<script type="text/javascript">
if(typeof dbApp === 'undefined'){
document.location.href="index.html";
}
</script>
<style>
table {
border: 1px solid #666;
width: 100%;
}
th {
background: #f8f8f8;
font-weight: bold;
padding: 2px;
}
</style>
<body>
<div class="jumbotron text-center">
<p>{{ message }}</p>
</div>
<div ng-controller="manageDevicesController">
<p> Click <a ng-click="loadPeople()">here</a> to load data.</p>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="person in people">
<td>{{person.id}}</td>
<td>{{person.firstName}}</td>
</tr>
</table>
</div>
</body>
似乎ng-click无法正常工作。知道为什么吗?我相信它在div中的ng-controller参考中是正确的!
答案 0 :(得分:0)
尝试重写您的loadPeople
功能,如下所示:
$scope.loadPeople = function() {
$http({
method: 'POST',
url: '/echo/json/',
data: dataForThisTest
}).then(function(data, status) {
$scope.people = data;
});
};