【发布时间】:2021-04-17 05:20:14
【问题描述】:
我需要从modalcomponent调用homecomponent的addItems函数。当我调用该函数时,该函数被调用但该行没有添加到html中。调用函数时我需要添加一个新的项目行。下面是我的代码 主页组件
import { Component, OnInit } from '@angular/core';
import { CommonComponent } from '../../common/common.component';
import $ from 'jquery';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor(public common:CommonComponent) { }
item_row: any[] = [];
ngOnInit(): void {
console.log(this.common.quote_company);
this.item_row.push({
'item_name':null,
'hsn_code':null,
'unit_price':null,
'qty':null,
'taxable_value':null,
'total':null
})
}
testJquery(){
$('.test').html('test');
}
addItems(){
this.addRow();
}
addRow(){
const new_row={
'item_name':'test',
'hsn_code':'test',
'unit_price':'test',
'qty':'test',
'taxable_value':'test',
'total':'test'
}
this.item_row.push(new_row);
console.log(this.item_row);
}
}
我正在从模态组件 html 调用 homecomponent 函数。调用此函数时,应将新行追加到表中。
模态组件
import { Component, OnInit ,EventEmitter, Output } from '@angular/core';
import { AddQuoteComponent } from '../add-quote/add-quote.component';
import { CommonComponent } from '../../common/common.component';
import { HomeComponent } from '../home/home.component';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {
constructor(
public home : HomeComponent,
public common :CommonComponent
) { }
ngOnInit(): void {
}
selectItem(){
console.log('test');
this.home.addItems();
}
}
【问题讨论】:
-
通常你 addRow() 在你的模式关闭事件。顺便说一句,有几种方法可以制作模态而不是 JQuery:例如ng-bootstrap:ng-bootstrap.github.io/#/components/modal/examples 或材质角度material.angular.io/components/dialog/overview
标签: angular binding components