- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在学习有关 Angular udemy 类(class),并遇到了这段代码。我有一个示例项目,它有一个添加项目按钮,可以在数组中添加一个新项目并在屏幕上显示更新的数组。
shopping-edit.component.ts
export class ShoppingEditComponent implements OnInit {
@Input() ingredientsArray : Ingredient[];
shoppingItemName : string = 'Test';
shoppingItemAmount : number = 66;
constructor(private shoppingListService : ShoppingListService) { }
ngOnInit() {
}
onItemAdd(){
console.log(this.shoppingItemName)
this.shoppingListService.addIngredient(this.shoppingItemName, this.shoppingItemAmount);
}
}
我有一个事件发射器,它在单击“添加”按钮时发射更新后的数组。
购物 list .service.ts
ingredientsChanged = new EventEmitter<Ingredient[]>();
addIngredient(name: string, value : number){
this.ingredients.push(new Ingredient(name, value));
this.ingredientsChanged.emit(this.ingredients.slice());
}
为了显示列表,我正在使用 shopping-list.component.ts
export class ShoppingListComponent implements OnInit {
constructor(private shoppingListService : ShoppingListService){}
ingredients : Ingredient[];
ngOnInit() {
this.ingredients = this.shoppingListService.getIngredients();
this.shoppingListService.ingredientsChanged.subscribe(
(ingredients : Ingredient[]) => {
this.ingredients = ingredients;
}
)
console.log("hello")
}
}
由于 shopping-list.component.ts 的 ngOnInit() 只运行一次,每次单击“添加”按钮时如何显示更新的列表?
最佳答案
您的列表未在 ngOnOInit
中更新。您只是在 ngOnInit
中订阅了一个可观察对象。因此,每当您在订阅中收到新数据时,您就会更新一个变量,因为 ngOnChanges
会被触发,这将更新您的 View 。
关于angular - 了解 ngOnInit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58164704/
在我的项目中,我想在 app.component 初始化期间调用一个 api,子组件应该等到 app.comonent 在 ngOnInit() 中完成 在 app.component.ts 中: n
在我的 NativeScript 应用程序中,我将路线设置为 export const routes = [ { path: "", component: AppComponent},
我有两个子组件,我想在它们之间传递数据,为此我使用服务。 在服务中我有 headerObject = new Subject(); 在第一个组件中我有方法 onClick() { thi
我有两个组件:TileComponent.ts 和 FullScreenComponent.ts。 单击 TileComponent 后,FullScreenComponent 将打开。在 TileC
在我定义它并对其进行排序后,我在控制台中收到一条错误消息,显示ERROR ReferenceError:sortedArr未定义。 这是我的 app.component.ts 文件: import {
我想通过 Angular 6 制作一个更新页面,首先从 webservice 加载数据并将其建立在 html form 中。我创建了一个 resovler 来处理异步数据加载,但是组件中的观察者总是返
我有一个 Controller 实现了 OnInit这里的问题是每当我更改路线并返回到同一组件时,每次都会调用 ngOnInit。我做错了什么我无法理解。任何人都请帮助我。 @Component({
当我调用构造函数中注入(inject)的服务时,我得到了未定义的信息。该服务在 ngOnInit 方法中调用,来自 Difference between Constructor and ngOnIni
当我导航到结果组件时,ngOnInit 中的服务按预期工作。然后我打开侧面菜单,导航到另一个页面,然后再次导航到结果页面。但是这次页面不呈现结果。而是呈现 ng-template。控制台上没有错误,什
我正在学习有关 Angular udemy 类(class),并遇到了这段代码。我有一个示例项目,它有一个添加项目按钮,可以在数组中添加一个新项目并在屏幕上显示更新的数组。 shopping-edit
Per Angular(https://angular.io/api/core/OnInit 表示 ngOnInit 在第一次检查指令的数据绑定(bind)属性之后,在检查其任何子项之前调用。它仅在指
我有一个类,它在初始化时从服务中检索数据并填充它的一个属性,它是一个数组。同一个类有一个函数可以对这个数组进行排序、过滤和返回。当我实例化此类的对象并调用此函数时,我意识到它是在其构造函数和 ngOn
Angular 4 应用程序。 我试图制作一个错误页面,以显示有关可能发生的未处理异常的一些信息。 GlobalErrorHandler 拦截最终错误并将用户重定向到由单个 ErrorComponen
只读属性只能在构造函数中赋值,但在 Angular 中,不鼓励甚至有时不可能使用构造函数进行某些初始化,而是使用 Angular 钩子(Hook) ngOnInit。有什么方法可以将 ngOnInit
我注意到当我返回到已经实例化的页面时,ngOnInit() 方法没有被调用。我必须为此使用任何其他方法吗?我需要一种方法,每次他访问特定页面时都会调用该方法。 编辑已经测试过 onPageWillEn
我有一个 Angular 2 应用程序,我使用路由器在 View 之间导航,就像其他人一样。以下是我的特定组件的路径: { path: 'home/view1/:viewID', co
我在同一页面上使用一些查询字符串参数调用 router.navigate。在这种情况下,ngOnInit() 不会调用。是默认设置还是我需要添加任何其他内容? 最佳答案 您可以注入(inject) A
我看过很多关于测试可观察对象的文章,包括 writing marble tests不知何故,我仍然找不到关于如何在我的 ngOnInit 周期中测试组件中的简单可观察对象的直接答案。 ngOnInit
我将 Angular 5 与 SortableJs 结合使用。 在一个页面上,我列出了多张分组的卡片。 HTML 看起来像 Edit 当我将卡片从一个组移到另一
请查看Stackblitz Editor关联。我已经设置了一个 Angular 应用程序。 Angular 应用程序的简要工作概述 两个组件,即应用组件和子组件 最初,子组件不显示。当我们点击按钮时,
我是一名优秀的程序员,十分优秀!