我们使用 Angular 为我们的应用程序执行类似/不同的行为。
html代码
<html>
<body ng-app="myApp">
<div ng-controller="newsCntrl" >
<form ng-submit="post()" name="postForm" id="postForm" >
<input type="submit" id="submit" value="Submit"/>
</form>
<a href="#" ng-click="likePost(post.id);">Like</a></span>
</div>
</body>
</html>
Controller 代码
myApp.controller("newsFeedCtrl", function ($scope, $http) {
console.log("inside newsFeedCtrl");
$scope.post() = function() {
console.log("posting");
}
$scope.likePost = function(postId) {
console.log("liking post");
}
});
问题是当我们点击“赞”时;它还调用了不应发生的 post 函数。
我们在这里遗漏了什么吗?
最佳答案
HTML 中的 Controller 名称不匹配:
<div ng-controller="newsCntrl" >
以及JS中的 Controller :
myApp.controller("newsFeedCtrl", function ($scope, $http)
如果我修复它,它似乎可以工作。 检查这个fiddle
关于javascript - anchor ng-click 调用 2 个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25104400/