我正在尝试把谷歌广告放在安古拉杰-重复如下所示。但它不起作用
<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有什么办法让adsense广告在ng-重复?
发布于 2014-06-18 14:13:32
您可以使用指令完成它。
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>发布于 2019-12-30 13:52:08
您还可以尝试在JS文件中创建这样的函数
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。
在窗口加载时调用此方法如下:
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');
};https://stackoverflow.com/questions/24267570
复制相似问题