gpt4 book ai didi

javascript - Angular.js 点击 json 数据数组?

转载 作者:行者123 更新时间:2023-11-28 19:45:52 25 4
gpt4 key购买 nike

我有一个小型投资组合项目,正在尝试使用 Angular 构建,我的投资组合中的项目使用 ng-repeat 重复。但我只想展示 1 个(第一个)项目。我在侧边栏中有一个小导航,我希望能够单击项目中的项目。我对 Angular 完全陌生,但到目前为止我已经阅读了路由和模板,但我不想使用外部 html 文件。非常感谢任何建议!

Javascript:

var app = angular.module('myPortfolio', []);

var pageContent = [
{
displayOrder: 1,
name: 'Project 1',
description: "Some description",
images: ["img/portfolio/feat_image_1.jpg"]
},
{
displayOrder: 2,
name: 'Project 2',
description: "A second description",
images: ["img/portfolio/feat_image_2.jpg"]
},
{
displayOrder: 3,
name: 'Project 3',
description: "A third description",
images: ["img/portfolio/feat_image_3.jpg"]
}
];

app.controller("portfolioController", function($scope) {
this.projects = pageContent;
});

HTML:

<div ng-controller="portfolioController as myPortfolio">

<!-- various html page header etc. -->

<div class="row" ng-repeat="project in myPortfolio.projects">

<div class="large-2 columns" id="sidebar">
<h1>{{project.name}}</h1>
<p>{{project.description}}</p>
<div class="row side-nav-div">
<div class="large-4 columns project-nav">
<button type="button" class="slick-prev slick-disabled left">Previous</button>
</div>
<div class="large-4 columns project-nav centered">
</div>
<div class="large-4 columns project-nav">
<button type="button" class="slick-next slick-disabled right" ng-click="">Next</button>
</div>
<div style="clear:both"></div>
</div>
</div>
<div class="large-10 columns" id="main-section">
<div class="slider1">
<div class="feature-img" ng-repeat="image in project.images">
<img ng-src="{{image}}"/>
</div>
</div>
</div>

</div>
</div>

最佳答案

最简单的解决方案:

app.controller("portfolioController", function($scope) {
$scope.projects = [
{
displayOrder: 1,
name: 'Project 1',
description: "Some description",
images: ["img/portfolio/feat_image_1.jpg"]
},
{
displayOrder: 2,
name: 'Project 2',
description: "A second description",
images: ["img/portfolio/feat_image_2.jpg"]
},
{
displayOrder: 3,
name: 'Project 3',
description: "A third description",
images: ["img/portfolio/feat_image_3.jpg"]
}
];

$scope.activeProject=0;

});

您的 HTML

<div ng-controller="portfolioController as myPortfolio">

<ul><li ng-repeat="project in myPortfolio.projects" ng-click="$parent.activeProject=$index">{{project.name}}</li></ul>
<!-- various html page header etc. -->

<div class="row" ng-repeat="project in myPortfolio.projects" ng-show="$index==$parent.activeProject">

<div class="large-2 columns" id="sidebar">
<h1>{{project.name}}</h1>
<p>{{project.description}}</p>
<div class="row side-nav-div">
<div class="large-4 columns project-nav">
<button type="button" class="slick-prev slick-disabled left" ng-disabled="$index==0" ng-click="activeProject=$index-1">Previous</button>
</div>
<div class="large-4 columns project-nav centered">
</div>
<div class="large-4 columns project-nav">
<button type="button" class="slick-next slick-disabled right" ng-disabled="$index==(myPortfolio.projects.length-1)" ng-click="activeProject=$index+1">Next</button>
</div>
<div style="clear:both"></div>
</div>
</div>
<div class="large-10 columns" id="main-section">
<div class="slider1">
<div class="feature-img" ng-repeat="image in project.images">
<img ng-src="{{image}}"/>
</div>
</div>
</div>
</div>
</div>

注意项目详细信息列表中添加的 activeProject 变量、ng-show 以及用于选择项目的项目名称菜单

关于javascript - Angular.js 点击 json 数据数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com