简体   繁体   English

Angularjs ng-repeat 必须只显示在一个<li>

[英]Angularjs ng-repeat must display only in a single <li>

I have a ng-repeat to display multiple projects.我有一个 ng-repeat 来显示多个项目。 When I click View Images button it will display images inside that project.当我单击“查看图像”按钮时,它将在该项目中显示图像。

The code works fine but it will add the html content of the images in all list of projects with a class ng-hide.该代码工作正常,但它会在所有项目列表中添加图像的 html 内容,并使用类 ng-hide。

How can I display the ng-repeat of the images in a single li only in the lists of project如何仅在项目列表中以单个li显示图像的 ng-repeat

projects.html项目.html

<ul>
    <li ng-repeat="item in projects">
        <p>{{ item.name }}</p>
        <p><button ng-click="displayImages(item.id)">View Images</button></p>
        <div ng-include="/static/images.html"></div>
    </li>
</ul>

images.html图片.html

<ul>
    <li ng-repeat="image in images">
        <p>{{ image.name }}</p>
        <img src="{{ image.src }}">
    </li>
</ul>

Controller控制器

$scope.displayImages = function(pid){
    req = {
        "method": "GET",
        "url": "/api/v1/" + pid + "/",
    }
    $http(req).success(function(response, status){
        $scope.images = response;
    })
}

You could set a scope variable such as activeProject and use that as a conditional in the view.您可以设置一个范围变量,例如activeProject并将其用作视图中的条件。 Will pass the whole item as argument of displayImages() .将整个item作为displayImages()参数传递。 Use ng-if to compare activeProject to each item使用ng-ifactiveProject与每个item进行比较

HTML HTML

<li ng-repeat="item in projects">
        <p>{{ item.name }}</p>
        <p><button ng-click="displayImages(item)">View Images</button></p>
        <!--  add ng-if conditional -->
        <div ng-if="item == activeProject" ng-include="/static/images.html"></div>
</li>

JS JS

$scope.displayImages = function(project){
    var pid = project.id;
    req = {
        "method": "GET",
        "url": "/api/v1/" + pid + "/",
    }
    $http(req).success(function(response, status){
        // assign the active project
        $scope.activeProject = project;
        $scope.images = response;
    })
}

You should use condition in image div to show proper project images.您应该在图像 div 中使用条件来显示正确的项目图像。

in projects.html:在projects.html中:

<ul>
    <li ng-repeat="item in projects">
        <p>{{ item.name }}</p>
        <p><button ng-click="displayImages(item.id)">View Images</button></p>
        <div ng-show="item.id === pid" ng-include="/static/images.html"></div>
    </li>
</ul>

also can use ng-if instead of ng-show也可以使用ng-if代替ng-show

in images.html: as usual but you can yse use ng-src instead of src在 images.html 中:像往常一样,但您可以使用ng-src而不是src

in controller: :在控制器中::

$scope.displayImages = function(pid){
    $scope.pid = pid;
    req = {
        "method": "GET",
        "url": "/api/v1/" + pid + "/",
    }
    $http(req).success(function(response, status){
        $scope.images = response;
    })
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM