Javascript 如何使用w3.css作为框架,以角度显示模式对话框?
我有一个创建元素列表的方法,我想显示一个关于点击元素的模式细节对话框,我使用w3.css作为css框架,但是我没有成功 我尝试使用NgStyle从家长到孩子的一些输入,4天前我使用angular,所以我还不习惯它的逻辑,在控制台日志中我看到我的点击正在触发,但我不知道之后发生了什么 我的代码 列出组件和模板Javascript 如何使用w3.css作为框架,以角度显示模式对话框?,javascript,css,angular,modal-dialog,w3-css,Javascript,Css,Angular,Modal Dialog,W3 Css,我有一个创建元素列表的方法,我想显示一个关于点击元素的模式细节对话框,我使用w3.css作为css框架,但是我没有成功 我尝试使用NgStyle从家长到孩子的一些输入,4天前我使用angular,所以我还不习惯它的逻辑,在控制台日志中我看到我的点击正在触发,但我不知道之后发生了什么 我的代码 列出组件和模板 import { Component, OnInit, ChangeDetectorRef } from '@angular/core'; import { BreakPointServic
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { BreakPointService } from '../../providers/break-point.service';
import { ReactiveTwitterSpringService } from '../../reactive/reactive-twitter-spring.service';
import { ITweet } from '../../reactive/model/itweet';
import { Subscription, Subject } from 'rxjs';
@Component({
selector: 'app-tweet-list',
templateUrl: './tweet-list.component.html',
styleUrls: ['./tweet-list.component.css']
})
export class TweetListComponent implements OnInit {
list_div_class;
search_input_class;
selectedTweet: ITweet;
public search_results$ = new Subject<any>();
search_results: ITweet[] = new Array();
subscribe: Subscription = new Subscription();
constructor(private tweetService: ReactiveTwitterSpringService, private cdr: ChangeDetectorRef) { }
visible = 'none';
search(tag) {
this.search_results = new Array();
this.subscribe.unsubscribe();
this.subscribe = new Subscription();
this.search_results$ = new Subject<any>();
this.subscribe.add(this.tweetService.search(tag).subscribe(tweet => {
this.search_results.push(tweet);
this.search_results = this.search_results.slice();
this.cdr.detectChanges();
this.search_results$.next(this.search_results);
console.log(tweet);
}));
console.log('array contains ' + this.search_results);
}
setSelected(tweet) {
console.log('selecting and showing');
this.selectedTweet = tweet;
this.visible = 'block';
}
ngOnInit() {
BreakPointService.current_css.subscribe(value => {
console.log('value is ' + value);
this.setupCss(JSON.parse(value));
});
}
setupCss(value: any): any {
this.list_div_class = value.list_div_class;
this.search_input_class = value.search_input_class;
}
}
import { Component, OnInit, Input } from '@angular/core';
import { ITweet } from '../../../../reactive/model/itweet';
@Component({
selector: 'app-tweet-detail',
templateUrl: './tweet-detail.component.html',
styleUrls: ['./tweet-detail.component.css']
})
export class TweetDetailComponent implements OnInit {
@Input() detail: ITweet = new ITweet();
@Input() visible = 'none';
constructor() { }
setInvisible() {
console.log('hidding');
this.visible = 'none';
}
ngOnInit() {
}
}
模板
<div class="{{list_div_class}}" style="max-height: 100vh;">
<input type="text" class="{{search_input_class}}" (keyup.enter)="search(searchInput.value)" #searchInput>
<ul class="w3-ul w3-hoverable" style="overflow-x:auto;max-height:70vh;">
<li class="w3-bar w3-center" *ngFor="let tweet of search_results " (click)="setSelected(tweet)">
<img src="{{tweet.userImage}}" class="w3-bar-item w3-circle" style="width:100px;">
<div class="w3-bar-item">
<span class="w3-large">{{tweet.id.name}}</span>
<br>
<span>#{{tweet.tag}}</span>
</div>
</li>
</ul>
<app-tweet-detail [detail]="selectedTweet" [visible]="visible"></app-tweet-detail>
</div>
<div id="modal" class="w3-modal" [ngStyle]="{display: visible}">
<div class="w3-modal-content">
<div class="w3-container" *ngIf="detail">
<span (click)='setInvisible()' class="w3-button w3-display-topright">×</span>
<img src='{{detail.userImage}}' style='width: 250px;' />
<span class='w3-large'>{{detail.id.name}} {{detail.country}} {{detail.placeFullName}}</span>
<p>{{detail.id.text}}</p>
</div>
</div>
</div>
&时代;
{{detail.id.name}{{detail.country}{{detail.placeFullName}}
{{detail.id.text}
我该怎么办
NB
在w3.css教程中,他们使用get元素by id将可见性更改为none或block。我想您需要做的是使用一个两个变量来保存
列表项和一个selectedItem
,您将在模式中显示。比如说
组件TS文件
因此,我们有了openmodel
,它在selectedItem
变量上为我们分配了一个项目,以显示特定的项目并让模式显示,关闭模式以撤销值并再次隐藏模式。所以在html上实现这一点看起来是这样的
组件HTML
openitem{{i}
&时代;
{{selectedItem.title}
{{selectedItem.content}
因此,我们有一个按钮,它在项目列表中循环,并将项目传递给函数,以便我们可以选择要显示的项目。然后在模式中,我们尝试检查是否定义了selectedItem
,如果是,则显示将可见,否则不显示
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
listOfItems: any[];
selectedItem: any;
ngOnInit() {
this.listOfItems = [
{
title: 'Title 1',
content: 'this is my first item'
},
{
title: 'Title 2',
content: 'this is my second item'
}
];
}
openModal( item ) {
this.selectedItem = item;
}
closeModal() {
this.selectedItem = undefined;
}
}
<button *ngFor="let item of listOfItems; let i = index" (click)="openModal(item)" class="w3-button">Open item #{{ i }}</button>
<div *ngIf="selectedItem" class="w3-modal" [style.display]="selectedItem ? 'block' : 'none'">
<div class="w3-modal-content">
<div class="w3-container">
<span (click)="closeModal()" class="w3-button w3-display-topright">×</span>
<h1>{{ selectedItem.title }}</h1>
<p>{{ selectedItem.content }}</p>
</div>
</div>
</div>