【发布时间】:2020-02-08 04:06:34
【问题描述】:
我有一些 ul li 列表,其数据来自循环。我的要求是,一旦我点击任何特定的 li,该 li 的图像应更改为第二张图像(dummyimage.com/300.png/09f/fff)像 active.Again 一样,即使我刷新页面,更改也应该在那里。下面是代码https://stackblitz.com/edit/angular-jmcye7?file=src%2Fapp%2Fapp.component.html
home.component.html
<div>
<ul>
<li style="border:1px solid;width:25%;margin-bottom:1%;cursor:pointer;padding:4px;" *ngFor="let items of statusdata" (click)="getData(items)"><span>{{items.id}}</span> <span>{{items.name}}</span>
<span><img [hidden]="nonactive" style="width:20px;height:20px;float:right" src ="/assets/1.png">
<img [hidden]="active" style="width:20px;height:20px;float:right" src ="/assets/2.png">
</span>
</li>
</ul>
</div>
home.component.ts
import { Component, OnInit } from '@angular/core';
import { CommonserviceService } from './../utilities/services/commonservice.service';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
declare var $: any;
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
getIt:any;
statusdata: any;
getOption:any;
myData:any;
myDataGet:any;
active:boolean = true;
nonactive:boolean = false;
ngOnInit() {
this.statusdata = [{ id: 1,name: 'Angular 2'},
{ id: 2, name: 'Angular 4'},
{ id: 3, name: 'Angular 5'},
{ id: 4, name: 'Angular 6'},
{ id: 5, name: 'Angular 7'}
];
console.log(this.getOption);
this.myDataGet = localStorage.getItem('dataSource');
if(this.myDataGet == 'true' ){
this.nonactive = true;
this.active = false;
}
else{
this.nonactive = false;
this.active = true;
}
}
getData(items){
this.myData = items.active;
localStorage.setItem('dataSource', this.myData);
this.myDataGet = localStorage.getItem('dataSource');
/* if(this.myDataGet == 'true' ){
this.nonactive = true;
this.active = false;
}
else{
this.nonactive = false;
this.active = true;
}*/
}
}
【问题讨论】:
-
能否请您创建一个堆栈闪电战来复制该问题?由于我们看不到图像,也无法真正判断点击事件是否都发生了变化。
-
++ 您需要更改数组
statusdata的每个对象的 active 属性 -
请找到demo stackblitz.com/edit/… ,您可以在浏览器中查看demo查看locastorage
-
this stackblitz 解决问题了吗?我没有完全理解本地存储问题(也许这个解决方案可以让你朝着正确的方向前进?)
-
不,实际上我的要求是,一旦我单击任何特定的 li,该 li 的图像应该更改为第二个图像(dummyimage.com/300.png/09f/fff),就像活动一样。即使我刷新页面,更改也应该在那里。我从 json 中删除了 active 属性,因为 active 属性应该自动添加到我们单击的 json 中,并且应该存储在 localstorage 中
标签: javascript html css angular