最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing param in ngOnInit in Angular2 - Stack Overflow

programmeradmin2浏览0评论

Is it possible to trigger a function that has a parameter in it like trigger(id) in ngOnInit?

I have a function that I want to trigger on (click) event as well as in ngOnInit so when the page first load it triggers basically the same function because it calls to the same API end-point.

function to be triggered in ngOnInit

 getOrdersFromOrigin(id: number): void {
   ...
}

so in ngOnInit would be something like this maybe? its showing an error "Cannot find name 'id'"

 ngOnInit() {
    this.getOrdersFromOrigin(id);
  }

Id is ing from my ngFor, its an Id of an item which I want to click (click)="getOrdersFromOrigin(id)"

Is it possible to trigger a function that has a parameter in it like trigger(id) in ngOnInit?

I have a function that I want to trigger on (click) event as well as in ngOnInit so when the page first load it triggers basically the same function because it calls to the same API end-point.

function to be triggered in ngOnInit

 getOrdersFromOrigin(id: number): void {
   ...
}

so in ngOnInit would be something like this maybe? its showing an error "Cannot find name 'id'"

 ngOnInit() {
    this.getOrdersFromOrigin(id);
  }

Id is ing from my ngFor, its an Id of an item which I want to click (click)="getOrdersFromOrigin(id)"

Share edited Dec 6, 2016 at 10:24 MrNew asked Dec 6, 2016 at 10:13 MrNewMrNew 1,4246 gold badges22 silver badges48 bronze badges 3
  • 2 id is part of the scope of getOrdersFromOrigin() so it would not be declared in the scope of ngInInit(). Unless you know which id to call you can't do it, id need to be set or a value need to be used instead. – GillesC Commented Dec 6, 2016 at 10:17
  • if id in not suppilied to the function it will throw error. You need to handle that in you function if id is not defined or present do something. Because in ngOnInit id might not be available as you are not triggering the click from the view/ its not user triggered. Pass some default value for that if not defined. – Manish Commented Dec 6, 2016 at 10:27
  • no I don't know which id i'm going to be clicking, as the ids are ing from ngFor and just passing the item.id in my function in (click) event. – MrNew Commented Dec 6, 2016 at 10:27
Add a ment  | 

2 Answers 2

Reset to default 2

You can handle it if its in route like param. For example if you are editing some record in table by clicking edit button then your app navigate to edit form you can handle it like this:

import { ActivatedRoute, Params, Router } from '@angular/router';

constructor(
    private route: ActivatedRoute
) { }

ngOnInit() {
    this.route.params.forEach((params: Params) => {
        let id = +params['id']; // (+) converts string 'id' to a number
        this.getOrdersFromOrigin(id);
    });
}

getOrdersFromOrigin(id: number){
//some code
};

The param id is in getOrdersFromOrigin(), so it would not work in the scope of ngOnInit(), my suggestion is that you can declare a property id and marked it as @Input(). then you can use it outside. The codes is below:

export class SameComponent implement OnInit {
   @Input() id:number=0; //initialize
    getOrdersFromOrigin(id: number): void {
    ...
  }
  ngOnInit() {   this.getOrdersFromOrigin(id);}
}
发布评论

评论列表(0)

  1. 暂无评论