首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Adsense广告在angularjs中不起作用-重复

Adsense广告在angularjs中不起作用-重复
EN

Stack Overflow用户
提问于 2014-06-17 15:18:43
回答 2查看 2K关注 0票数 4

我正在尝试把谷歌广告放在安古拉杰-重复如下所示。但它不起作用

代码语言:javascript
复制
<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>

我在控制台中看到了下面的错误。

代码语言:javascript
复制
Uncaught Error: adsbygoogle.push(): All ins elements in the DOM with class=adsbygoogle already have ads in them

有什么办法让adsense广告在ng-重复?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-18 14:13:32

您可以使用指令完成它。

代码语言:javascript
复制
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({});
        }
    };
});

在html侧

代码语言:javascript
复制
<div  google-adsense>
</div>
票数 4
EN

Stack Overflow用户

发布于 2019-12-30 13:52:08

您还可以尝试在JS文件中创建这样的函数

代码语言:javascript
复制
function createAndAppendAdsElement(id, adUnitID) {
    var parent = document.getElementById(id);

    var ele = document.createElement('ins');
    ele.style.display = 'block';
    ele.className = 'adsbygoogle';
    ele.setAttribute('data-ad-client', 'ca-pub-XXXXXXXX');
    ele.setAttribute('data-ad-slot', adUnitID);
    ele.setAttribute('data-ad-format', 'auto');
    ele.setAttribute('data-full-width-responsive', 'true');

    parent.appendChild(ele);

    (adsbygoogle = window.adsbygoogle || []).push({});
}

此方法查找带有ID的div,然后创建INS元素并将其附加到该div。

在窗口加载时调用此方法如下:

代码语言:javascript
复制
window.onload = function () {
    createAndAppendAdsElement('elementOne', 'your_ad_unit_number');
    createAndAppendAdsElement('elementTwo', 'your_ad_unit_number');
    createAndAppendAdsElement('elementThree', 'your_ad_unit_number');
    createAndAppendAdsElement('elementFour', 'your_ad_unit_number');
    createAndAppendAdsElement('elementFive', 'your_ad_unit_number');
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24267570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档