tschumann
tschumann

Reputation: 3246

Stop AngularJS untemplated request

In my template I have something like the following:

<img data-ng-if="$ctrl.shouldShowFacebookPixel" 
  src="https://www.facebook.com/tr?id={{$ctrl.facebookPixelId}}&amp;ev=PageView"
 />

When I load the page I can see in my dev tools a request is made to

https://www.facebook.com/tr?id={{$ctrl.facebookPixelId}}&amp;ev=PageView

Then a bit later a request is made to the correct URL after the variable has been substituted.

https://www.facebook.com/tr?id=blah&amp;ev=PageView

How can I stop the initial request before variable substitution has happened?

Upvotes: 0

Views: 24

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Go for ng-src instead src:

<img data-ng-if="$ctrl.shouldShowFacebookPixel" 
  ng-src="{{'https://www.facebook.com/tr?id='+ $ctrl.facebookPixelId + '&amp;ev=PageView'}}"
 />

Angular needs to compile the URL, and your browser has no clue what is {{/* ... */}}. This is a reason why you see

https://www.facebook.com/tr?id={{$ctrl.facebookPixelId}}&amp;ev=PageView  

Upvotes: 2

Related Questions