Skip to content

Instantly share code, notes, and snippets.

@dmarcelino
Created May 22, 2015 11:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmarcelino/3f83371d120b9600eda3 to your computer and use it in GitHub Desktop.
Save dmarcelino/3f83371d120b9600eda3 to your computer and use it in GitHub Desktop.
Angular adsense example that shows ad more than 3 times
'use strict';
var adSenseTpl = '<div id="adsenses-ad-1"><ins class="adsbygoogle"' +
'style="display:inline-block;width:336px;height:280px"' +
'data-ad-client="ca-pub-000000000000000000"' +
'data-ad-slot="00000000000"></ins></div>';
var adsenseAd1;
angular.module('MyApp')
.directive('adsense', function ($compile) {
return {
restrict: 'EA',
replace: true,
link: function (scope, element, attrs) {
element.on('$destroy', function() {
if(adsenseAd1) {
adsenseAd1.detach();
console.log('destroy: detached ad1');
}
});
if(adsenseAd1) {
element.append(adsenseAd1);
console.log('attached ad1');
} else {
adsenseAd1 = $compile(adSenseTpl2)(scope);
element.append(adsenseAd1);
(adsbygoogle = window.adsbygoogle || []).push({});
console.log('added template to DOM');
}
}
};
});
/**
* HTML
* <div data-adsense></div>
* /
@maximeshr
Copy link

Thanks you save my life >.<

Juste replace adSenseTpl2 with adSenseTpl at l.28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment