我有一个显示博客文章的页面。我正在尝试通过ng-click传递一个删除函数。但是,当我点击超链接(ng-click)时,它不会从控制器调用该函数。控制器已正确加载,并使用console.log确保页面正在成功加载相关控制器。有任何想法吗?谢谢你们!AngularJS - 控制器功能无法使用w/ng点击
HTML瓦特/ NG-点击(about.html):
<table class="table table-bordered table-striped" ng-show="post.posts">
<thead>
<tr>
<th>_id</th>
<th>Title</th>
<th>Body</th>
<th>Created</th>
<th>Updated</th>
<th class="col-sm-2"></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="post in post.posts | orderBy: 'createdAt'">
<td>{{ post._id }}</td>
<td>{{ post.title }}</td>
<td>{{ post.body }}</td>
<td>{{ post.createdAt | date : "MMMM d, y h:mm a" }}</td>
<td>{{ post.updatedAt | date : "MMMM d, y h:mm a" }}</td>
<td class="col-sm-2">
<a ng-href="/about/{{ post._id }}" class="btn btn-danger">Edit</a>
<!-- ============================
DELETE BUTTON THAT IS NOT WORKING
--> =============================
<a ng-href="#" ng-click="post.deletePost(post._id)" class="btn btn-primary">Delete</a>
</td>
</tr>
</tbody>
</table>
控制器:
angular.module('postCtrl', [])
.controller('postController', function(Post) {
var vm = this;
// grab all posts at page load
Post.all()
.success(function(data) {
// bind the posts that come back to vm.posts
vm.posts = data;
});
// =============================
// FUNCTION I ATTEMPTING TO CALL
// ==============================
vm.deletePost = function(id) {
Post.delete(id)
.success(function(data) {
// get all posts to update the table
Post.all()
.success(function(data) {
vm.posts = data;
});
});
};
});
API端点(与邮递员测试,工作正常):
// DELETE request to delete a post by ID
apiRouter.route('/posts/:post_id')
// delete the user with this id
.delete(function(req, res) {
Post.remove({
_id: req.params.post_id
}, function(err, post) {
if (err) res.json({ message: "Error: " + err});
res.json({ message: 'Successfully deleted' });
});
});
角度路线:
.when('/about', {
templateUrl: 'app/views/pages/posts/about.html',
controller: 'postController',
controllerAs: 'post'
});
你能分享可执行演示/片断或[的jsfiddle(https://jsfiddle.net/)? [_创建一个最小,完整和可验证的示例_](http://stackoverflow.com/help/mcve) – Rayon
您是否使用controller作为控制器的语法? –