我试图遍历一个数组,该数组包含一个带有对象作为值的数组的键。这是包含数组的组件
sidemenu-links.ts
import { SideMenuLink } from './sidemenu-link';
export const SIDEMENULINKS: SideMenuLink[] = [
{
"linkTitle": "Getting Started",
"linkRoute": "introduction",
"subLinks": [
{
"linkTitle": "Introduction",
"linkRoute": "introduction"
},
{
"linkTitle": "Download",
"linkRoute": "download"
},
{
"linkTitle": "Contents",
"linkRoute": "contents"
},
{
"linkTitle": "Browser & devices",
"linkRoute": "browser-and-devices"
},
{
"linkTitle": "JavaScript",
"linkRoute": "javascript"
},
{
"linkTitle": "Theming",
"linkRoute": "theming"
},
{
"linkTitle": "Build tools",
"linkRoute": "build-tools"
},
{
"linkTitle": "Webpack",
"linkRoute": "webpack"
},
{
"linkTitle": "Accessibility",
"linkRoute": "accessibility"
}
]
},
{
"linkTitle": "Layout",
"linkRoute": "layout",
"subLinks": []
},
{
"linkTitle": "Content",
"linkRoute": "content",
"subLinks": []
},
{
"linkTitle": "Components",
"linkRoute": "components",
"subLinks": []
},
{
"linkTitle": "Utilities",
"linkRoute": "utilities",
"subLinks": []
},
{
"linkTitle": "Extend",
"linkRoute": "extend",
"subLinks": []
},
{
"linkTitle": "Migration",
"linkRoute": "migration",
"subLinks": []
},
{
"linkTitle": "About",
"linkRoute": "about",
"subLinks": []
},
]
这是sidemenu.component
sidemenu.component.ts
import { Component, OnInit } from '@angular/core';
import { NavLink } from '../nav-link';
import { SIDEMENULINKS } from '../sidemenu-links';
@Component({
selector: 'app-sidemenu',
templateUrl: './sidemenu.component.html',
styleUrls: ['./sidemenu.component.scss']
})
export class SidemenuComponent implements OnInit {
sideMenuLinks = SIDEMENULINKS;
linkSelected: NavLink;
constructor() { }
expandLink(link:any): void {
this.linkSelected = link;
}
ngOnInit() {
}
}
这是我尝试使用* ngFor
的sidemenu.component.htmlsidemenu.component.html
<div class="side-menu-container #sidemenucontainer">
<ul>
<li *ngFor="let link of sideMenuLinks">
<div class="nav-link" [class.selected]="link === linkSelected" (click)="expandLink(link)">{{link.linkTitle}}</div>
<div *ngIf="link === linkSelected">
<div class="nav-link" *ngFor="let link of sideMenuLinks">{{link.subLinks.linkTitle}}</div>
</div>
</li>
</ul>
</div>
当我运行它时,我点击它打开的任何侧链接,
答案 0 :(得分:1)
您只需要在嵌套*ngFor
中包含<div class="side-menu-container #sidemenucontainer">
<ul>
<li *ngFor="let link of sideMenuLinks">
<div class="nav-link" [class.selected]="link === linkSelected" (click)="expandLink(link)">{{link.linkTitle}}</div>
<div *ngIf="link === linkSelected">
<div class="nav-link" *ngFor="let sub of link.subLinks">{{sub.linkTitle}}</div>
</div>
</li>
</ul>
</div>
数组。以下是使用对象结构和html的简化StackBlitz Demo代码。
#include <stdio.h>
int main()
{
char s[10000]; // sentence
char array[100][100]; // array where i put every word
printf("Insert sentence: "); // receive the sentece
gets(s);
printf("%s",s);
int i = 0;
int j = 0;
int k = 0;
for(j = 0; s[j] != '\0'; j++){ // loop until i reach the end
if ( s[j] != ' ' || s[j] == '\0' )
{
array[i][k] = s[j];
k++;
}
else {
i++;
k = 0;
}
}
return 0;
}