将ng-click处理程序添加到angularjs中的重复元素

时间:2013-08-07 23:12:23

标签: javascript angularjs angularjs-ng-repeat

我使用angularjs 1.0.7并尝试将ng-click处理程序添加到使用ng-repeat重复的列表元素,但该函数不会触发。下面是html,以及我在评论中尝试过的变体。我尝试在不同的元素上添加相同的ng-click函数,它工作正常。

<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>

这是控制器

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);

    };

});

1 个答案:

答案 0 :(得分:2)

标记中有错误,控制器中的函数称为getPlayList,标记中称为getPlaylist。请注意l上的大小写。这是一个小提琴,它是固定的,没有问题: http://jsfiddle.net/GYatesIII/z7mpW/3/