Created
May 22, 2015 11:07
-
-
Save dmarcelino/3f83371d120b9600eda3 to your computer and use it in GitHub Desktop.
Angular adsense example that shows ad more than 3 times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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> | |
* / |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks you save my life >.<
Juste replace adSenseTpl2 with adSenseTpl at l.28