在 ng-click 不调用部分控制器功能时遇到困难
Having difficulty with an ng-click not calling controller function in partial
我花了几天时间查看 ng-click 未调用此处控制器函数的其他答案,但我仍然无法弄清楚我做错了什么。
我有一个主要的script.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 参考中是正确的
尝试像这样重写您的 loadPeople
函数:
$scope.loadPeople = function() {
$http({
method: 'POST',
url: '/echo/json/',
data: dataForThisTest
}).then(function(data, status) {
$scope.people = data;
});
};
我花了几天时间查看 ng-click 未调用此处控制器函数的其他答案,但我仍然无法弄清楚我做错了什么。
我有一个主要的script.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 参考中是正确的尝试像这样重写您的 loadPeople
函数:
$scope.loadPeople = function() {
$http({
method: 'POST',
url: '/echo/json/',
data: dataForThisTest
}).then(function(data, status) {
$scope.people = data;
});
};