无法使用子方法触发器调用父方法

法里斯·卡波

在 angular 应用程序的一种情况下,我无法使用子触发器执行该方法。我在父方法和子方法中都放置了一个控制台日志,根本只调用了子方法。父级有一个类型对象的参数,所以我试图删除参数只是为了查看是否可以调用该函数在我看来问题似乎位于 (myChildMethod)="myParentMethod()"但我不知道为什么在应用程序的另一个实例中我已经设法做到这一点,所以我试图复制它并检查是否有任何差异,但我看到的那些,例如间距似乎无关紧要。我已将我认为与问题相关的代码部分标记为“重要” 但是我已经发布了整个组件,以防问题出在我不知道应该查看的地方。

子购物编辑组件 HTML 是:

<div class="row">
    <div class="col-xs-12">
        <form >
            <div class="row">
                <div class="col-sm-5 form-group">
                    <label for="name">Name</label> 
                    <input type="text" 
                    id="name" 
                    class="form-control"
                    #nameInput
                    >
                </div>
                <div class="col-sm-2 form-group">
                    <label for="amount">amount</label>
                    <input type="number" 
                    id="amount" 
                    class="form-control"
                    #amountInput>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <!-- IMPORTANT -->
                    <button class="btn btn-success"
                        type="button"
                        (click)="addNew()">Add</button>
                    <button class="btn btn-danger" 
                    type="button">Delete</button>
                    <button class="btn btn-primary" type="button">Clear</button>
                </div>
            </div>
        </form>
    </div>
</div>

子购物编辑组件 TS 是:

import { Component, OnInit, Output, EventEmitter } from '@angular/core';

import { Component, OnInit, ElementRef, Output, EventEmitter, ViewChild } from '@angular/core';
import { Ingredient } from "../../shared/ingredient.model"

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

  @ViewChild('nameInput', {static: false}) nameInputRef: ElementRef;
  @ViewChild('amountInput', {static: false}) amountInputRef: ElementRef;

  @Output() emitIngredient = new EventEmitter<Ingredient>(); 
  // IMPORTANT 
  addNew(nameEl: ElementRef, amountEl:  ElementRef){
    console.log("addNew");
    const newIngredient = new Ingredient(
       this.nameInputRef.nativeElement.value,
       this.amountInputRef.nativeElement.value
    );

        this.emitIngredient.emit(newIngredient);
  }

  constructor() { 
  }

  ngOnInit() {

  }

}

父购物清单组件 html 是:

<div class="row">
    <div class="col-xs-10">
        <!-- IMPORTANT -->
        <app-shopping-edit  (addNew)= "addNewIngredient($event)"></app-shopping-edit>
        <hr>
        <ul class="list-group">
            <a class="list-group-item" 
            style="cursor: pointer"
            *ngFor="let ingredient of ingredients">
            {{ ingredient.name }} , {{ ingredient.amount}}  
        </a>
        </ul>
    </div>
</div>

而父购物清单组件 ts 是:

import { Component, OnInit, Input } from '@angular/core';
import { Ingredient } from '../shared/ingredient.model';
@Component({
  selector: 'app-shopping-list',
  templateUrl: './shopping-list.component.html',
  styleUrls: ['./shopping-list.component.css']
})
export class ShoppingListComponent implements OnInit {

  ingredients: Ingredient[] = [
    new Ingredient('Apples', 5),
    new Ingredient('Potato', 3)

  ];
  // IMPORTANT
  addNewIngredient(newIngredient: Ingredient){
    console.log("addNewIngredient");
    this.ingredients.push(newIngredient);
  }

  constructor() { }

  ngOnInit() {
  }

}

我在一个案例中成功地在孩子和父母之间传递了数据。子标题组件 Html。下面的所有代码都适用于应用程序的工作示例。

    <div class="navbar-default">
            <ul class="nav navbar-nav">
                <li (click)="onRecipeSelected('recipe')"><a href="#">Recipes</a></li>
                <li (click)="onListSelected('list')"><a href="#">Shopping List</a></li>
            </ul> 
</div>

Child Header Component Ts 

从“@angular/core”导入 { Component, EventEmitter, Output };

@Component({ selector: 'app-header', templateUrl: "./header.component.html" }) 导出类 HeaderComponent {

    @Output() featureSelected = new EventEmitter<string>();

    onRecipeSelected(feature: string){

        this.featureSelected.emit(feature);
    }

    onListSelected(feature: string){
        this.featureSelected.emit(feature)
    }

}

父应用组件 HTML

<div class="row">
        <div class="col-mid-12">
          <app-recipes 
          *ngIf="loadedFeature === 'recipe'"></app-recipes>
          <app-shopping-list 
          *ngIf="loadedFeature === 'list'"></app-shopping-list>
        </div>
    </div>

父应用组件 Ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'RecipeApp';
  value = 10;



   loadedFeature = "recipe";

   onNavigate(feature: string){

     this.loadedFeature = feature;
   }
 }

这工作正常。父组件被称为传递的值。

应用模块文件是:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { RecipesComponent } from './recipes/recipes.component';
import { RecipeListComponent } from './recipes/recipe-list/recipe-list.component';
import { RecipeItemComponent } from './recipes/recipe-list/recipe-item/recipe-item.component';
import { ShoppingListComponent } from './shopping-list/shopping-list.component';
import { ShoppingEditComponent } from './shopping-list/shopping-edit/shopping-edit.component';
import { RecipeDetailComponent } from './recipes/recipe-detail/recipe-detail.component';
import { HighlighterDirective } from './highlighter/highlighter.directive';
import { UnlessDirective } from './highlighter/unless.directive';
import { DropdownDirective } from './shared/dropdown.directive';
import { FormsModule } from '@angular/forms';  

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    RecipesComponent,
    RecipeListComponent,
    RecipeItemComponent,
    ShoppingListComponent,
    ShoppingEditComponent,
    RecipeDetailComponent,
    HighlighterDirective,
    UnlessDirective,
    DropdownDirective
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
罗伯特·加西亚

您忘记在 HTML 中添加您的事件,其中包含您从子组件发出的值

<div class="row">
<div class="col-xs-10">
    <!-- IMPORTANT -->
    <app-shopping-edit  (addNew)= "addNewIngredient($event)"></app-shopping-edit>
    <hr>
    <ul class="list-group">
        <a class="list-group-item" 
        style="cursor: pointer"
        *ngFor="let ingredient of ingredients">
        {{ ingredient.name }} , {{ ingredient.amount}}  
    </a>
    </ul>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用事件触发器时,不会触发Slider Thumb.DragStarted事件或调用方法

来自分类Dev

使用交互触发器调用可见性已更改的方法WPF

来自分类Dev

子触发器也会运行父触发器吗?

来自分类Dev

使用InheritedWidget在子级中触发触发器动作

来自分类Dev

为什么无法在onOpen触发器中调用UrlFetchApp?

来自分类Dev

使用jQuery触发器无法正确触发keydown事件

来自分类Dev

我们可以从mysql触发器调用servlet或java方法吗?

来自分类Dev

我们可以从mysql触发器调用servlet或java方法吗?

来自分类Dev

无法通过指令调用父控制器方法

来自分类Dev

从触发器中修改子控件,还是通过更好的方法来更改其前景?

来自分类Dev

无法更新存储函数/触发器中的表“ blad”,因为调用该存储函数/触发器的语句已使用该表

来自分类Dev

MySQL错误:无法更新存储函数/触发器中的表'tbl',因为调用该存储函数/触发器的语句已使用该表

来自分类Dev

#1442-无法更新存储函数/触发器中的表“ INSURANCE”,因为调用该存储函数/触发器的语句已使用该表

来自分类Dev

(MySQL) 无法更新存储函数/触发器中的表“PARTS”,因为它已被调用此存储函数/触发器的语句使用

来自分类Dev

继承 - 调用子方法而不是父方法

来自分类Dev

如何使父/包装组件“触发”或在子组件上调用方法?

来自分类Dev

使用模板加载作为服务器方法Meteor js的触发器

来自分类Dev

来自父控件的样式触发器

来自分类Dev

使用父控制器方法调用测试Angular

来自分类Dev

单击子元素上的jQuery单击父元素上的触发器

来自分类Dev

phpMyAdmin创建触发器,以在不存在子行时删除父行

来自分类Dev

jQuery在单击子级时禁用父级触发器

来自分类Dev

MySQL:无法在触发器中使用SIGNAL

来自分类Dev

无法在SQL中使用触发器删除行

来自分类Dev

MySQL:无法在触发器中使用SIGNAL

来自分类Dev

无法使用触发器更新正确的总价

来自分类Dev

无法使用 Crashlytics 触发器部署函数

来自分类Dev

我可以将getActiveRange方法与OnFormSubmit触发器一起使用吗?

来自分类Dev

如何重写此触发器以使用游标或基于集合的方法?

Related 相关文章

  1. 1

    使用事件触发器时,不会触发Slider Thumb.DragStarted事件或调用方法

  2. 2

    使用交互触发器调用可见性已更改的方法WPF

  3. 3

    子触发器也会运行父触发器吗?

  4. 4

    使用InheritedWidget在子级中触发触发器动作

  5. 5

    为什么无法在onOpen触发器中调用UrlFetchApp?

  6. 6

    使用jQuery触发器无法正确触发keydown事件

  7. 7

    我们可以从mysql触发器调用servlet或java方法吗?

  8. 8

    我们可以从mysql触发器调用servlet或java方法吗?

  9. 9

    无法通过指令调用父控制器方法

  10. 10

    从触发器中修改子控件,还是通过更好的方法来更改其前景?

  11. 11

    无法更新存储函数/触发器中的表“ blad”,因为调用该存储函数/触发器的语句已使用该表

  12. 12

    MySQL错误:无法更新存储函数/触发器中的表'tbl',因为调用该存储函数/触发器的语句已使用该表

  13. 13

    #1442-无法更新存储函数/触发器中的表“ INSURANCE”,因为调用该存储函数/触发器的语句已使用该表

  14. 14

    (MySQL) 无法更新存储函数/触发器中的表“PARTS”,因为它已被调用此存储函数/触发器的语句使用

  15. 15

    继承 - 调用子方法而不是父方法

  16. 16

    如何使父/包装组件“触发”或在子组件上调用方法?

  17. 17

    使用模板加载作为服务器方法Meteor js的触发器

  18. 18

    来自父控件的样式触发器

  19. 19

    使用父控制器方法调用测试Angular

  20. 20

    单击子元素上的jQuery单击父元素上的触发器

  21. 21

    phpMyAdmin创建触发器,以在不存在子行时删除父行

  22. 22

    jQuery在单击子级时禁用父级触发器

  23. 23

    MySQL:无法在触发器中使用SIGNAL

  24. 24

    无法在SQL中使用触发器删除行

  25. 25

    MySQL:无法在触发器中使用SIGNAL

  26. 26

    无法使用触发器更新正确的总价

  27. 27

    无法使用 Crashlytics 触发器部署函数

  28. 28

    我可以将getActiveRange方法与OnFormSubmit触发器一起使用吗?

  29. 29

    如何重写此触发器以使用游标或基于集合的方法?

热门标签

归档