3

I am creating tables rows when the http.get method updates me ... on receiving data I create table rows using JS / jquery in the angular 2 version.

My Code:

<tr>
    <td>2</td>
    <td>BAJAJ-AUTO</td>
    <td>14.284%</td>
    <td>27/12/2013 12:00 am</td>
    <td>30/12/2013 12:00 am</td>
    <td>1935</td>
    <td>30/12/2013 12:00 am</td>
    <td>1935</td>
    <td>31/12/2013 12:00 am</td>
    <td>2120</td>
    <td><button class="btn btn-default" onclick="processAdvise('BAJAJ-AUTO')">Process Advise</button></td>
</tr>

so the last the td - has a button which shall call my angular 2 function to process it - This code doesnt reaches even at the start of the function

I also tried this to no avail:

  • (click) for angular 2
  • onclick and kept the function in the same HTML template using script tag
3
  • Can you provide more information about what you try to accomplish? Commented Jun 27, 2016 at 11:46
  • i want the click event to fires up a angular 2 function ... but the click event code in html is created dynamically ... (which is based on http service of angular 2) Commented Jun 27, 2016 at 11:53
  • i want to .. fill the table row data into another form which is present on that page - so basically the JS function will read the row and then fill up the required html form Commented Jun 27, 2016 at 11:55

2 Answers 2

6

Angular2 doesn't process HTML outside of a components template in any way, therefore it is expected (click)="processAdvise('BAJAJ-AUTO') doesn't work.

onclick="processAdvise('BAJAJ-AUTO')" also won't work when processAdvise() is a method of an Angular2 component because onclick is HTML-only and functions assigned this way are searched in the global JS scope not inside a components class.

<script> tags are remove from Angular2 templates

@Component({
  selector: '...',
  ....
})
class MyComponent {
  constructor(private elRef:ElementRef) {
  }

  addHtml() {
    // add the HTML to the DOM
    this.elRef.nativeElement.querySelector('button').addEventListener('click', (event) => this.handleClick(event));
  }

  handleClick(event) {
    // doSomething();
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

so what is the possible solution to overcome this problem ?
See my comment below your question.
let me tryout this and update after it - Thanx in advance :)
I tried this solution not working for me.Did any one find the solution means please provide.
"Didn't not work" doesn't provide information anyone can act on. I'd suggest you create a new question with the code that demonstrates what you tried, and what the expected and actual behavior is (and the error message if there is one)
|
2

Here is a simple solution I tried and it worked. Assign your component object to Window object in the constructor like the following:

...
constructor(private elementRef: ElementRef, private emService: SomeService) {
Window["myComponent"] = this;}

onClickAJS(){alert('it works!')}
...

Then you can access members of the component from DOM onclick="Window.myComponent.onClickAJS()". Also, you can call it from the browser console like : Window.myComponent.onClickAJS(), should work as well. It is a hackish way. More appropriate way is to create a component for your generated HTML and load it at run time. Here is the article to get you started. dynamic components

2 Comments

whast is myComponent
myComponent is the key string to store/retrieve the saved value ('this' is the value in that case).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.