AngularJS-使用Jquery将下拉菜单功能添加到HTML表中



我想添加一个功能(它是一个下拉菜单,位于表中的图像图标中(。以下是代码描述:

<HTML>
<!--Table-->
<div id="tableStructure"...>
<table...>
<thread>
<tr><td class="people"></td></tr></thread></table>
...
</HTML>

这是控制器(脚本(

$scope.buildtable = function() {
...
$(".people", row).html("<img src='" + person.imageURL + "'/>");
...

所以,在表格上,每一行都会显示一个图像图标,我的问题是当你点击图标时,如何添加下拉菜单。我的菜单是这样的:

<div ng-controller="Controller">
<li> List 1 </li>
...
<div>

首先,我不熟悉AngularJS。

当我看到你的代码时,我觉得很有趣,因为你使用AngularJS,但实际上你使用的是".html((",这是jQuery方法。根本没有束缚。

就我个人而言,如果"jQuery"one_answers"AngularJS"是有组织的,我认为使用它们是没有问题的。我想出了这两个主意
1.使用过滤器使用相同的数组。

<!-- table(when the img is clicked, you update its state.  -->
<tr ng-repeat="people in peopleList">
<td>{{people.name}}</td>
<td><img ng-src="{{people.imageUrl}}" ng-click="imgClick($index)"/></td>
</tr>
<!-- list(use the same array using filter) -->
<li ng-repeat="people in peopleList | filter : {state : '1'}">
<img ng-src="{{people.imageUrl}}" />
</li>

2.为列表准备数据。

<!-- table(when the img is clicked, you push its data to listItems.  -->
<tr ng-repeat="people in peopleList">
<td>{{people.name}}</td>
<td><img ng-src="{{people.imageUrl}}" ng-click="imgClick($index)"/></td>
</tr>
<!-- list -->
<li ng-repeat="item in listItems">
<img ng-src="{{item.imageUrl}}" />
</li>

这里有一个非常粗略的例子。

更新
哇。。。也许我被误解了。您想要使用普通的HTML表(由jQuery.HTML((创建(。在这种情况下,您可以使用此代码。

// called when the img is clicked
$(".table").on("click", ".people-image", function() {
// Get the scope(you need to select which has ng-controller).
var $scope = angular.element($('#your-app-controller')).scope();
// Add img url.
$scope.listItems.push({ imageUrl : $(this).attr("src") });
// Apply it to view. (It seems this is bad practice, though.)
$scope.$apply();
});

下面是一个例子。

最新更新