AdSense ads do not work inside angularjs ng-repeat

I am trying to place Google Adsense ads inside Angularjs ng-repeat, as shown below. But its not working

<div ng-repeat="item in items"
<div ng-include src="view.getItem($index)"></div>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<div ng-if="$index == 0" google-adsense>
    <ins class="adsbygoogle responsive"
         style="display:inline-block;width:468px;height:60px"
         data-ad-client="ca-pub-"
         data-ad-slot=""></ins>
    <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
</div>
</div>

I see below error in my console.

Uncaught Error: adsbygoogle.push(): All ins elements in the DOM with class=adsbygoogle already have ads in them

Can I show adsense ads inside ng-repeat?

+4
source share
1 answer

You can do this with the directive

var adSenseTpl = '<ins class="ad-div adsbygoogle responsive" style="display:inline-block;width:468px;height:60px" data-ad-client="ca-pub-xxxxx" data-ad-slot="xxxxx"></ins></ins>';

angular.module('reviewmattersApp')
    .directive('googleAdsense', function($window, $compile) {
    return {
        restrict: 'A',
        transclude: true,
        template: adSenseTpl,
        replace: false,
        link: function postLink(scope, element, iAttrs) {
                element.html("");
                element.append(angular.element($compile(adSenseTpl)(scope)));
                if (!$window.adsbygoogle) {
                    $window.adsbygoogle = [];
                }
                $window.adsbygoogle.push({});
        }
    };
});

In html side

<div  google-adsense>
</div>
+4
source

Source: https://habr.com/ru/post/1544932/


All Articles