Search This Blog

2024/05/20

Running Javascript function from Angular component

 


we can run javascript from angular code on certain event.

For that first create a javascript file inside asset folder.

Mine is located at src/assets/js/selectPickerTrigger.js

Inside this js file add following code

function reRenderSelect(){
alert("I M IN")
}

Go to angular.json file search for script tag & add path to your js file here.

"scripts": [
"src/assets/js/selectPickerTrigger.js"
]

Now go to your name.component.ts file

add following to list of import statements.

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
declare function reRenderSelect(): any;

Now you can call our reRenderSelect() function from anywhere in component.ts
code as

onCountrySelected(value: string) {
reRenderSelect()
}


Here I am running reRenderSelect() function from a event handler for select
change,you can run it from anywhere in name.component.ts

No comments:

Post a Comment