Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jul 10, 2015

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
You can’t perform that action at this time.