Convert string to executable code

hello guys
i want use eval on ionic actions but not working exactly

# alert sample
let string1="alert('test message')"
# nav sample 

If you can back up a bit and describe your overall goal, perhaps we can help do it in a more Angularly way.

for sample i want write a function for nav controller with ‘goToPage’ name

# page can include my ionic pages like home , aboutUs , contactus , Products,etc
#in view
      <button ion-item (click)="goToPage(contactUs)">
contact us
        <ion-icon name="person" item-left></ion-icon>

i think i most use eval for convert string to real class name

Nope. The easiest way would be to use lazy page loading, where your gotoPage() method can be pretty much as-is, and all you have to do is quote ‘contactUs’ in the template. If you don’t want to do that, then you can make a map of strings to class constructors like so:

pages = {
  'contactUs': contactUs,

gotoPage(pageName: string): void {
1 Like

mmmm, this is not good ,on angular\ionic i cant convert string to exec code ? this is only a sample, i have more sample for needed eval

another sample : i want build a element from server ( on black friday i want build a offer element on home page or something like this ) without update application from play store

In my opinion, it’s not really as much “can’t” as “shouldn’t”. Obviously you’re free to ignore everything I say, but I think you’re fighting the framework. eval is a really dangerous tool. Frameworks such as Angular, as well as browsers, have been consistently attempting to discourage this sort of approach in favor of something that can be more reliably secured.

One of the very common topics that comes up here over and over again is a variant of “how can I do what I used to with ng-bind-html these days?”. The Angular team deliberately made that very hard to do, and I think did so with good reason. I was initially frustrated with this, it made me rethink my design, and now I am firmly convinced that that was a good thing. Hopefully you will be able to do the same and come to the same conclusion.

1 Like