我使用 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);
};
});