Javascript – Adding ng-click handler to repeated element in angularjs

angularjsangularjs-ng-repeatjavascript

Im using angularjs 1.0.7 and trying to add a ng-click handler to a list element which is repeated using ng-repeat, but the function does not fire. Below is the html, along with variations I have tried in comments. I have tried adding the same ng-click function on a different element and it works fine.

<div class="results" ng-cloak ng-show="showList != 0">

    <ul>
        <li ng-repeat="movie in movies" ng-click="getPlaylist(movie.id)">{{ movie.title }}</li>
        <!--<li ng-repeat="movie in movies" ng-click="getPlaylist({{movie.id}})">{{ movie.title }}</li>-->
        <!--<li ng-repeat="movie in movies" ng-click="getPlaylist('movie.id')">{{ movie.title }}</li>-->
        <!--<li ng-repeat="movie in movies" ng-click="getPlaylist(\'{{movie.id}}\')">{{ movie.title }}</li>-->
    </ul>
</div>

And here is the controller

main.controller('MoviesListCtrl', function($scope, $http, playlist) {

    $scope.movies = [];
    $scope.showList = false;


    $scope.getMoviesList = function (val) {

        var link = 'titles?q=' + val + '&limit=10'

        $http.get(link).success(function (data) {
            // now we have all our movies and can add them
            $scope.movies = data;

            // if there any movies we can show the list
            if ($scope.movies.length > 0) {
                $scope.showList = true;
            } else {
                $scope.showList = false;
            }
        });
    };

    $scope.getPlayList = function (id) {
        alert(id);

    };

});

Best Answer

You have an error in your markup, your function in the controller is called getPlayList and in your markup its getPlaylist. Note the capitalization on the l. Here's a fiddle where it's fixed and there's no problem: http://jsfiddle.net/GYatesIII/z7mpW/3/