简体   繁体   English

如何使用w3.css作为框架以角度显示模式对话框?

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

I have an angular NgFor that is creating a list of elements, I want to show a modal detail dialog about the clicked element, I m using w3.css as css framework, but I m not successfull 我有一个角度NgFor正在创建元素列表,我想显示有关单击元素的模态详细对话框,我将w3.css用作css框架,但未成功

I tried to use the NgStyle with some input from parent to child, I strated using angular 4 days ago so I m not yet used to it logic, in the console logs I see that my click is firing but I dont know what is happening after 我尝试将NgStyle与父母到孩子的输入一起使用,我在4天前使用angular进行了分级,所以我还不习惯于此逻辑,在控制台日志中,我看到我的点击被触发,但是之后我不知道发生了什么

My code 我的密码

List Component and Template 列出组件和模板

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

template 模板

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

My detail component and template 我的详细信息组件和模板

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() {
    }

}

template 模板

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

What should I do? 我该怎么办?

NB NB

In the w3.css tutorial they use the get element by id to change the visibility to none or block. 在w3.css教程中,他们使用id的get元素将可见性更改为none或block。

I guess what you need to do is have a two variables holding the listOfItems and a selectedItem where you will show on the modal. 我想您需要做的是有两个变量,分别保存listOfItemsselectedItem ,您将在模态中显示它们。 so for example 例如

Component TS file 组件TS文件

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

}

so we have openModal which assigns an item on the selectedItem variable for us to display the specific item and let the modal show, closeModal to revoke the value and make our modal hide again. 因此,我们有openModal ,它在selectedItem变量上分配了一个项目以供我们显示特定的项目,并让模式显示,closeModal撤消该值并再次使我们的模式隐藏。 so implementing this on the html looks like this. 因此,在html上实现它看起来像这样。

Component HTML 组件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>

So we have a button that loops thru the list of items and pass the item on the function so we could select which item to be shown. 因此,我们有一个按钮可循环通过项目列表,并将该项目传递给函数,以便我们选择要显示的项目。 Then in the modal we try to check if selectedItem is defined, so if yes then the display will be visible and not otherwise. 然后,在模式中,我们尝试检查是否定义了selectedItem ,因此,如果是,则显示将是可见的,否则将不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM