如何使用 w3.css 作为框架在 angular 中显示模态对话框?

How To show a modal dialog in angular, using w3.css as framework?

我有一个 angular NgFor 正在创建一个元素列表,我想显示一个关于被点击元素的模式细节对话框,我使用 w3.css 作为 css 框架, 但我没有成功

我尝试将 NgStyle 与父子之间的一些输入一起使用,我在 4 天前使用 angular 进行了说明,所以我还不习惯它的逻辑,在控制台日志中我看到我的点击是射击,但我不知道

之后发生了什么

我的代码

列出组件和模板

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;
    }
}

模板

<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>

我的详情组件和模板

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 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">&times;</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>

我该怎么办?

NB

在 w3.css 教程中,他们使用通过 id 获取元素来将可见性更改为 none 或块。

我想你需要做的是有两个变量保存 listOfItems 和一个 selectedItem ,你将在模式上显示。例如

Component TS file

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;
  }

}

所以我们有openModal,它在selectedItem变量上分配一个项目,让我们显示特定项目并让模态显示,closeModal 撤销该值并使我们的模态再次隐藏。所以在 html 上实现这个看起来像这样。

Component HTML

<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">&times;</span>
      <h1>{{ selectedItem.title }}</h1>
      <p>{{ selectedItem.content }}</p>
    </div>
  </div>
</div>

所以我们有一个循环遍历项目列表并将项目传递给函数的按钮,这样我们就可以 select 显示哪个项目。然后在模式中,我们尝试检查是否定义了 selectedItem,如果是,则显示将可见,否则不可见。