Raichu Raichu - 8 months ago 60
AngularJS Question

anchor ng-click calling 2 methods

We are using angular to do like/unlike behavior for our app.

Code for html

<body ng-app="myApp">
<div ng-controller="newsCntrl" >
<form ng-submit="post()" name="postForm" id="postForm" >
<input type="submit" id="submit" value="Submit"/>
<a href="#" ng-click="likePost(post.id);">Like</a></span>

Code for controller

myApp.controller("newsFeedCtrl", function ($scope, $http) {
console.log("inside newsFeedCtrl");

$scope.post() = function() {
$scope.likePost = function(postId) {
console.log("liking post");


The problem is when we click on like; it also calls post function which should not be happening.

Are we missing anything here?

Answer Source

There is a mismatch between the controller name in the HTML:

  <div ng-controller="newsCntrl" >

and the controller in the JS:

  myApp.controller("newsFeedCtrl", function ($scope, $http)

If I fix it, it seems to work. Check this fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download