1

ng-click attribute not firing the function provided.

HTML:

<div class="row">
        <div style="display:inline-block;width:20%;height:25%;margin:20px;"  ng-repeat="news in dailyNews" >
            <div class="thumbnail" ng-click="showNews({{news.id}})">
                <img src="{{news.image}}" alt="images/noimg.jpg" style="width:100%">
                <div class="caption" >
                    <h3  >{{news.heading}}</h3>
                </div>
            </div>
        </div>
    </div>

ROUTING SCRIPT:

.state('dashboard.user',{
        url:'/user-dashboard',
        templateUrl:'views/user.html',
        controller:'userPanelCtrl'
    })

CONTROLLER:

.controller('userPanelCtrl',function($state,$scope,userServices){
            userServices.checkSession();
            $scope.showNews = function(id){
                localStorage.setItem("show-news",id);
            }
        })

I got the data successfully and loaded them to the html page using ng-repeat but the ng-click does not work on any of the div repeated. Thanks in advance.

2 Answers 2

3

Please change this line :

<div class="thumbnail" ng-click="showNews({{news.id}})">

to

<div class="thumbnail" ng-click="showNews(news.id)">

ng-click is angularjs directive , so you dont need to write news.id like {{}} you can directly pass as it will recognize your function showNews similarly it will recognize your user.id too.

Sign up to request clarification or add additional context in comments.

4 Comments

is not it the same thing posted above?
@Sajeetharan 2 minutes apart, probably started writing as you submitted yours... Plus, correcting code is 1 thing, you failed to explain why OP's code was wrong, which is how OP will actually learn.
Figured that the next minute I posted. Thanks a lot.
@Sajeetharan . I was in middle of my answer when your answer popped up . I did not see your answer , i posted mine and then saw yours .
1

You do not have to pass the expression as an argument, just remove {{}}

From

 <div class="thumbnail" ng-click="showNews({{news.id}})">

To

 <div class="thumbnail" ng-click="showNews(news.id)">

1 Comment

Figured that the next minute I posted. Thanks a lot.