gpt4 book ai didi

angularjs - Adsense 广告在 angularjs ng-repeat 中不起作用

转载 作者:行者123 更新时间:2023-12-03 20:23:17 33 4
gpt4 key购买 nike

我正在尝试将 Google Adsense 广告放入 Angularjs ng-repeat 中,如下所示。但它不起作用

<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>

我在控制台中看到以下错误。
Uncaught Error: adsbygoogle.push(): All ins elements in the DOM with class=adsbygoogle already have ads in them

有没有办法在 ng-repeat 中显示 Adsense 广告?

最佳答案

你可以使用指令来做到这一点

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 端
<div  google-adsense>
</div>

关于angularjs - Adsense 广告在 angularjs ng-repeat 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267570/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com