6

I have an array of objects. i am iterating that in loop and passing each item's name to onclick which targets a function openIt(val) in app.js file which is in assets folder. ie

Angular Code:

<div *ngFor="let item of listArray">
<button class="tabopen" onclick="openIt(item.name)">{{item.name}}</button>
</div>

app.js Code:

function openIt(data) {
console.log(data);
}

In my openIt function in app.js file i am not getting item.name. In my console it displays error that item is not defined. But when i pass static data i.e onclick="openIt('sample_data')"it does not show any error.

Even though item.name also exists i am getting correct values against this as well. I am not getting that why i am not able to pass iterative data to the parameters.

7
  • use (click)="function". Commented Jan 8, 2020 at 6:25
  • I want to target the function in my app.js file which is not in the same component. When i use (click) it looks for the openIt() in my .ts file and displays openIt is not a function Commented Jan 8, 2020 at 6:29
  • So basically you want to call a function of another component, You can use shared service See this example (stackblitz.com/edit/angular-shared-service) Commented Jan 8, 2020 at 6:32
  • @SameerKhan no i want my component to communicate with a simple app.js file in my assets folder Commented Jan 8, 2020 at 6:51
  • @AhmadHabib are you using angularJs or angular>=2 Commented Jan 8, 2020 at 6:58

3 Answers 3

12

If you're using Angular then you should go with (click) because when you declare an event handler you need to surround the DOM event name in parentheses and assign a template statement to it.

<button class="tabopen" (click)="openIt(item.name)">{{item.name}}</button>
Sign up to request clarification or add additional context in comments.

5 Comments

But, won't it look for the function in same component's TS file then? I want to target a function in app.js file due to some reasons
@AhmadHabib if the previous version works for you then this should work as well because I just change the event handler with angular version of click
I tired this but it says openIt() is not defined as it is looking for this function in same component. I am still wanting to target openIt() function in my app.js file located in assets folder.
@AhmadHabib, then you should declare a function in your current component which calls another function from app.js
That worked. I called a function in current component with (click) event and from that event i called openIt() that was in my app.js file. Thank you so much mate. Much appreciation
2

event binding: varsion 1 on-click="function", version 2 (click)="function"

<div *ngFor="let item of listArray">
<button class="tabopen" on-click="openIt(item.name)">{{item.name}}</button>
</div>

<div *ngFor="let item of listArray">
<button class="tabopen" (click)="openIt(item.name)">{{item.name}}</button>
</div>

Comments

0

Yoc can also send item and get all properties of item in .ts file.

Html file:

<button class="tabopen" (click)="openIt(item)">{{item.name}}</button>

ts class:

 openIt(item){
        consol.log('item==>' , item)
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.