代码之家  ›  专栏  ›  技术社区  ›  Mohammed Housseyn Taleb

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

  •  0
  • Mohammed Housseyn Taleb  · 技术社区  · 6 年前

    我有一个角度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>
    

    我该怎么办?

    注意

    在w3.css教程中,他们使用get element by id将可见性更改为none或block。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Riyenz    6 年前

    我想你需要做的是让两个变量保持 listOfItems 和A selectedItem 您将在模式中显示的位置。例如

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

    所以我们有 openModal 它在 选择项目 变量用于显示特定项并让模式显示,closemodal用于撤消值并使模式再次隐藏。所以在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>
    

    所以我们有一个按钮,可以循环浏览项目列表并在函数上传递项目,这样我们就可以选择要显示的项目。然后在模态中,我们尝试检查 选择项目 已定义,因此如果是,则显示将可见,否则不可见。