要解决Angular 1.2不显示Adsense广告的问题,你可以尝试以下解决方法:
标签将Adsense代码嵌入到你的页面中。
请确保将YOUR-AD-CLIENT-ID
和YOUR-AD-SLOT-ID
替换为你自己的广告客户端ID和广告槽ID。
$sce
服务来信任和解析Adsense代码。在你的控制器中注入$sce
服务,并将Adsense代码包装在$sce.trustAsHtml()
方法中:
app.controller('AdsenseController', ['$scope', '$sce', function($scope, $sce) {
$scope.adsenseCode = $sce.trustAsHtml('');
}]);
然后,在HTML模板中使用ng-bind-html
指令将Adsense代码绑定到一个元素上:
$timeout
服务来延迟加载Adsense代码。在你的控制器中注入$timeout
服务,并在延迟一段时间后加载Adsense代码:
app.controller('AdsenseController', ['$scope', '$timeout', function($scope, $timeout) {
$timeout(function() {
// 加载Adsense代码
(adsbygoogle = window.adsbygoogle || []).push({});
}, 100);
}]);
确保将延迟时间设置为足够长的时间,以确保Angular编译完成后再加载Adsense代码。
使用这些解决方法,你应该能够在Angular 1.2中正确地显示Adsense广告。