简体   繁体   English

Ng Bootstrap-如何从另一个组件另一个组件调用模式组件?

[英]Ng Bootstrap - how to call modal Component from another component another component?

I am a newbie and learning angular . 我是一个新手,正在学习angular I have a background of angularjs . 我有angularjs的背景。 I have structure of my app like this. 我有这样的我的应用程序的结构。

// src /
//  app /
//       app-footer
//                  app-footer.component.css , app-footer-component.html,app-footer.component.ts
//       app-header
//                 app-header.component.css,app-header.component.html,app-header.ts           
//       home
//            home.component.css,home.component.html,home.component.ts
//       shared 
//              modals
//                    todo
//                         update-todo.css,update-todo.html,update-todo.ts

I am using ng-bootstrap and with the help of it i am showing a modal on click. 我正在使用ng-bootstrap并借助它在单击时显示模式。 My home.component.html looks like this. 我的home.component.html看起来像这样。

<div class="container home-page">
<div class="jumbotron text-center">
  <h1>I'm a Todo-aholic
  </h1>
</div>
<div id="todo-form" class="row">
  <div class="mx-auto col-sm-8 text-center">
    <form action="">
      <div class="form-group">
        <input type="text" name="something" [(ngModel)]="todo"  class="form-control input-lg text-center"  placeholder="I want to buy a puppy that will love me forever">
        <div class="add-button-todo-app">
          <button class="btn btn-primary" (click)="pushInTodoArr(todo) " [disabled]="todo =='' ? true : false">ADD</button>
        </div>
      </div>
    </form>
  </div>
</div>
<div class="list-of-todos">
  <ul>
    <li *ngFor="let item of todoArray;let i=index">
      <span class="tick">
        <i class="fa fa-check" aria-hidden="true"></i>
      </span>
      {{item}}
      <span class="trash" (click)="removeItemInTodoArr(item);">
        <i class="fa fa-trash" aria-hidden="true"> </i>
      </span>
      <span class="trash" (click)="content.openLg(content)">
        <i class="fa fa-pencil" aria-hidden="true"> </i>
      </span>
    </li>
  </ul>
</div>

<update-todo></update-todo>

and app.component.ts looks like this. app.component.ts看起来像这样。

    import { Component, OnInit } from '@angular/core';
import { NgbdModalOptions } from '../shared/modals/todo/update-todo';


@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  /* todo array list */
  todoArray: String[] = [];
  todo: string = "";

  constructor() {

  }

  ngOnInit() {
  }

  /* Push data in todo array */
  pushInTodoArr(value) {
    this.todoArray.push(value);
    this.todo = '';
  }

  /* remove item in array */
  removeItemInTodoArr(key) {
    for (var i = this.todoArray.length - 1; i >= 0; i--) {
      if (this.todoArray[i] === key) {
        this.todoArray.splice(i, 1);
      }
    }
  }

  /* update item in array */
  updateItemInTodoArr(key,updatedValue) {
    for (var i = this.todoArray.length - 1; i >= 0; i--) {
      if (this.todoArray[i] === key) {
        this.todoArray[i] = updatedValue;
      }
    }
  }
}

Its basically a todo app. 它基本上是一个待办事项应用程序。 Which adds delete and updates. 其中添加了删除和更新。 I want to update the field in modal. 我想以模态更新字段。

This is my update-todo.ts . 这是我的update-todo.ts

import {Component, ViewEncapsulation} from '@angular/core';

import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'update-todo',
  templateUrl: './update-todo.html',
  encapsulation: ViewEncapsulation.None,
  styleUrls: ['./update-todo.css']
})
export class NgbdModalOptions {
  closeResult: string;

  constructor(private modalService: NgbModal) {}

  openLg(content) {
    this.modalService.open(content, { size: 'lg' });
  }

}

Now I want to open the pop up and pass the value on click update icon. 现在,我想打开弹出窗口并在单击更新图标时传递值。 i am calling this function (click)="content.openLg(content)" but i get Cannot read property 'openLg' of undefined . 我正在调用此函数(click)="content.openLg(content)"但我Cannot read property 'openLg' of undefined

Will some one please point me to right direction. 请问有人给我指出正确的方向。 I am stumbling between ng-bootstrap , ngx-bootstrap and ng2-bootstrap . 我在ng-bootstrapngx-bootstrapng2-bootstrap之间ng2-bootstrap But i want to do it with ng-bootstrap . 但是我想用ng-bootstrap做到这一点。

This is how I did it in my app: Import: 这是我在应用程序中执行的操作:导入:

import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';

constructor: 构造函数:

constructor(        
        private _modalService: NgbModal
    ) { }

Call: 呼叫:

onEdit(car: ICars) {
        const modalRef = this._modalService.open(RentACarDetailComponent, { size: 'lg' });
        modalRef.componentInstance.car = car;
        modalRef.componentInstance.type = 'edit';
        modalRef.componentInstance.pageTitle = 'Edit vehicle';
        modalRef.result.then((result) => {
            if (result) {
                let answer: number;
                this._rentACarService.editVehicle(result, 2)
                    .takeUntil(this.ngUnsubscribe)
                    .subscribe(
                    result => { answer = result; },
                    error => { this.errorMessage = <any>error; },
                    () => {answer > 0) {
                            this._helperService.showInfo('You have succesfully editet vehicle.', 5);
                            this.ngOnInit();
                        }
                    });
            }
        },
            (reason) => { });
    }

car, type and PageTitle are set as Inputs in modal component car, type and PageTitle被设置为模式组件中的Inputs

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

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