- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个 Angular 应用程序,它对 Drupal CMS 系统进行 ajax 调用以获取一些内容。其中一些内容包括来自 Facebook、Twitter、Instagram 和 Youtube 的小部件。
ajax 调用是通过 postsController
完成的。和 postsService
.
我无法让这些社交媒体小部件正确呈现。当有人直接进入带有小部件的页面时,我已经让它们正确呈现,但如果它们 更改路线 (页面 View )小部件不会加载。我相信这是因为 API 使用的脚本仅在页面加载事件上运行。
起初, Angular 根本没有渲染小部件,但我做了两件事让它工作。
/platform.twitter.com/widgets.js
compileAjax
的自定义指令更改了从 Drupal 获取的数据,我对它进行了 Angular 重新编译。 html
中代码的屏幕截图和示例在单个
html
中复制问题的文档博士。小部件在页面加载时正确呈现,但在您单击“转到页面”链接之一时不正确。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<script src="https://code.angularjs.org/1.5.9/angular-sanitize.min.js"></script>
<script src="https://code.angularjs.org/1.5.9/angular-route.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.min.js"></script>
<script>
var app = angular.module('app', ['ngSanitize','ngRoute']);
app.config(function($routeProvider) {
$routeProvider.when('/page1', {
controller: 'postsController as postsCtrl',
templateUrl: 'page1.htm'
}).when('/page2', {
controller: 'postsController as postsCtrl',
templateUrl: 'page2.htm'
})
.otherwise({ redirectTo: '/page1' });
});
app.controller('postsController', ['postsService',function(postsService) {
var postsCtrl = this;
postsCtrl.test = 'this is an expression from the controller'
var promise = postsService.getPost(1);
promise.then(function(data) {
postsCtrl.data = data.data;
// I can't access the API on SO so am replicating what I would get from it here.
postsCtrl.twitter = "<div data-oembed-url=\"https:\/\/twitter.com\/NatGeo\/status\/811610711671656448\">\n<div style=\"max-width:320px;margin:auto;\">\n<blockquote align=\"center\" class=\"twitter-tweet\">\n<p dir=\"ltr\" lang=\"en\" xml:lang=\"en\">Polar bears are just one of the animals that will benefit from President Obamas\u00a0recent ban on oil drilling <a href=\"https:\/\/t.co\/MX4ZnX7TNw\">https:\/\/t.co\/MX4ZnX7TNw<\/a><\/p>\n\u2014 National Geographic (@NatGeo) <a href=\"https:\/\/twitter.com\/NatGeo\/status\/811610711671656448\">December 21, 2016<\/a><\/blockquote>\n<script async=\"\" charset=\"utf-8\" src=\"\/\/platform.twitter.com\/widgets.js\"><\/script><\/div>\n<\/div>";
postsCtrl.facebook = "<div data-oembed-url=\"https:\/\/www.facebook.com\/natgeo\/posts\/10154212815083951\">\n<div id=\"fb-root\">\u00a0<\/div>\n<script>\n<!--\/\/--><![CDATA[\/\/ ><!--\n(function(d, s, id) {\n var js, fjs = d.getElementsByTagName(s)[0];\n if (d.getElementById(id)) return;\n js = d.createElement(s); js.id = id;\n js.src = \"\/\/connect.facebook.net\/en_US\/sdk.js#xfbml=1&version=v2.3\";\n fjs.parentNode.insertBefore(js, fjs);\n}(document, 'script', 'facebook-jssdk'));\n\/\/--><!]]>\n<\/script><div class=\"fb-post\" data-href=\"https:\/\/www.facebook.com\/natgeo\/posts\/10154212815083951\" data-width=\"550\">\n<blockquote cite=\"https:\/\/www.facebook.com\/natgeo\/posts\/10154212815083951\" class=\"fb-xfbml-parse-ignore\">\n<p>Inhumane and unsafe, snake wine is often made by drowning a live snake in alcohol. Before you purchase a gift abroad, here are a few things to know.<\/p>\nPosted by <a href=\"https:\/\/www.facebook.com\/natgeo\/\">National Geographic<\/a> on\u00a0<a href=\"https:\/\/www.facebook.com\/natgeo\/posts\/10154212815083951\">Wednesday, December 21, 2016<\/a><\/blockquote>\n<\/div>\n<\/div>"
postsCtrl.instagram = "<div data-oembed-url=\"https:\/\/www.instagram.com\/p\/BOTfVSFDgn-\/?taken-by=natgeo&hl=en\">\n<div style=\"max-width:320px;margin:auto;\"><!-- You're using demo endpoint of Iframely API commercially. Max-width is limited to 320px. Please get your own API key at https:\/\/iframely.com. -->\n<blockquote class=\"instagram-media\" data-instgrm-version=\"7\" style=\" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);\">\n<div style=\"padding:8px;\">\n<div style=\" background:#F8F8F8; line-height:0; margin-top:40px; padding:50.0% 0; text-align:center; width:100%;\">\n<div style=\" background:url(data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAABGdBTUEAALGPC\/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURczMzPf399fX1+bm5mzY9AMAAADiSURBVDjLvZXbEsMgCES5\/P8\/t9FuRVCRmU73JWlzosgSIIZURCjo\/ad+EQJJB4Hv8BFt+IDpQoCx1wjOSBFhh2XssxEIYn3ulI\/6MNReE07UIWJEv8UEOWDS88LY97kqyTliJKKtuYBbruAyVh5wOHiXmpi5we58Ek028czwyuQdLKPG1Bkb4NnM+VeAnfHqn1k4+GPT6uGQcvu2h2OVuIf\/gWUFyy8OWEpdyZSa3aVCqpVoVvzZZ2VTnn2wU8qzVjDDetO90GSy9mVLqtgYSy231MxrY6I2gGqjrTY0L8fxCxfCBbhWrsYYAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;\">\u00a0<\/div>\n<\/div>\n\n<p style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;\"><a href=\"https:\/\/www.instagram.com\/p\/BOTfVSFDgn-\/\" style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none;\" target=\"_blank\">A photo posted by National Geographic (@natgeo)<\/a> on <time datetime=\"2016-12-22T03:35:07+00:00\" style=\" font-family:Arial,sans-serif; font-size:14px; line-height:17px;\">Dec 21, 2016 at 7:35pm PST<\/time><\/p>\n<\/div>\n<\/blockquote>\n<script async=\"\" defer=\"defer\" src=\"\/\/platform.instagram.com\/en_US\/embeds.js\"><\/script><\/div>\n<\/div>\n\n<p>\u00a0<\/p>";
postsCtrl.youtube = "<div data-oembed-url=\"https:\/\/youtu.be\/G51LtqmZKto\">\n<div style=\"max-width:320px;margin:auto;\"><!-- You're using demo endpoint of Iframely API commercially. Max-width is limited to 320px. Please get your own API key at https:\/\/iframely.com. -->\n<div>\n<div style=\"left: 0px; width: 100%; height: 0px; position: relative; padding-bottom: 56.2493%;\"><iframe allowfullscreen=\"\" frameborder=\"0\" src=\"https:\/\/www.youtube.com\/embed\/G51LtqmZKto?wmode=transparent&rel=0&autohide=1&showinfo=0&enablejsapi=1\" style=\"top: 0px; left: 0px; width: 100%; height: 100%; position: absolute;\" tabindex=\"-1\"><\/iframe><\/div>\n<\/div>\n<\/div>\n<\/div>\n\n<p>\u00a0<\/p>\n<\/div>\n \n<\/div>"
postsCtrl.slider = "<div class=\"slick-slider\">\n \n <div>Service Slide 1<\/div>\n \n <div>Service Slide 2<\/div>\n \n <div>Service Slide 3<\/div>\n \n <\/div>";
});
}]);
app.service("postsService", function($http, $q) {
function getPost(postsId) {
var deferred = $q.defer()
var url = 'https://jsonplaceholder.typicode.com/albums/' + postsId;
$http({
method: 'GET', // GET OPTIONS
cache: true,
url: url,
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function(response) {
//your code when success
deferred.resolve(response);
}, function(response) {
//your code when fails
deferred.reject(response);
});
return deferred.promise;
}
this.getPost = getPost;
});
app.directive('compileAjax', function($compile) {
return {
restrict: 'A',
replace: true,
link: function(scope, elem, attrs) {
scope.$watch(attrs.compileAjax, function(html) {
elem[0].innerHTML = html;
$compile(elem.contents())(scope);
});
}
}
});
app.directive('slickSlider', function() {
return {
restrict: 'C',
link: function(scope, elem, attrs) {
$(elem).slick({
// settings
});
}
}
});
</script>
<script src="//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.5"
async></script>
<script async="" defer="defer" src="//platform.instagram.com/en_US/embeds.js"></script>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</head>
<body>
<body ng-app="app" ng-controller="postsController as postsCtrl">
<script type="text/ng-template" id="page1.htm">
<h2>You're on Page 1</h2>
<a href="#page1">Go to page 1</a>
<a href="#page2">Go to page 2</a>
<div compile-ajax="postsCtrl.twitter"></div>
<div compile-ajax="postsCtrl.facebook"></div>
<div compile-ajax="postsCtrl.instagram"></div>
<div compile-ajax="postsCtrl.youtube"></div>
<div compile-ajax="postsCtrl.slider"></div>
</script>
<script type="text/ng-template" id="page2.htm">
<h2>You're on Page 2</h2>
<a href="#page1">Go to page 1</a>
<a href="#page2">Go to page 2</a>
<div compile-ajax="postsCtrl.twitter"></div>
<div compile-ajax="postsCtrl.facebook"></div>
<div compile-ajax="postsCtrl.instagram"></div>
<div compile-ajax="postsCtrl.youtube"></div>
<div compile-ajax="postsCtrl.slider"></div>
</script>
<div ng-view></div>
</body>
</body>
</html>
最佳答案
您需要在加载嵌入小部件后手动初始化它们的 javascript,因为小部件是在页面加载后添加的,因此不会被初始化。
对于Twitter :
twttr.widgets.load();
If content is dynamically inserted into a page (such as lazy-loading content or using a
pushState
technique to navigate between articles) it’s necessary to request Twitter’s widgets JavaScript scan for new buttons and widgets using thetwttr.widgets.load()
function.
instgrm.Embeds.process();
If you want to load the library separately from the HTML code, you can call the oEmbed endpoint with the
omitscript
parameter. This is useful for websites that want to handle the loading of the embeds.js script by themselves. To manually initialize the embed code, you can then call the JavaScript functioninstgrm.Embeds.process()
.
FB.XFBML.parse();
You can use this function to re-render social plugins using either the XFBML syntax (e.g.
<fb:like>
) or the HTML5 syntax (e.g.<div class="fb-like">
)
关于angularjs - Angular JS 无法在页面/路由更改时呈现社交媒体小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278998/
假设我有父页面。 当我单击“打印”时,会打开一个新选项卡(页面)以进行打印预览。 我想要的是,当我单击“打印”时,父选项卡(页面)上应显示覆盖层,以停止进一步的工作,直到子选项卡关闭。 Because
我知道这是不可能的,但我想我还是会问这个问题。 我有一个 HTTPS 页面,并且正在动态创建一个表单。我想将表单发布到 HTTP 页面。在浏览器不弹出警告的情况下这可能吗?当我在 IE8 上执行此操作
在我的应用程序中,我实现了ajax 4.0客户端模板 目前我的模板位于同一个.aspx页面上。 (例如 Main.aspx) 但我想将它们外部化。(即所有 HTML 都会放在另一个页面上) 为此,我使
我目前正在构建自己的网络应用程序。在此应用程序的标题中有一个导航栏。 我将相同的导航栏添加到我的所有 html 页面,以便在页面之间导航时保持可见,并将导航元素设置为事件到我现在所在的页面。 我的
我们有一个在集成管道下运行的 IIS7 Intranet 站点,该管道主要是 ASP.Net,带有一些传统的经典 ASP 页面。该站点允许匿名访问大多数区域,但使用 Windows 身份验证来保护某些
有一个要求,我需要根据 div 标签的高度超过 700 像素的条件将大型 HTML 页面拆分为多个页面。 我还可以根据字数拆分页面。 下面的示例根据 HTML 标签分割页面,而我需要根据 div 高度
有两个 html 页面,如果在第一个页面中单击特定图像,它将转到第二个 html 页面。如果在第二个 html 页面上的任何地方单击它应该返回到第一页并将第二页完全显示在第一页的一个 div 中 1s
我有一个简单的 hello world Servant 应用程序。我需要向它添加一些静态或动态的 html 页面。我怎样才能做到这一点?在文档中没有提到它。注意我不想在 Haskell 代码中创建 h
我有两个文件 index.html 和 index2.html。这两个文件都在本地机器上的同一目录中(无法访问 PHP 等)。 我正在努力 Page Title 使用 jQuery 从 index.h
假设我有一个 JSP 页面 index.jsp User id:
我面临着一个对我来说非常陌生的情况。我会尽力让事情变得简单来解释它一步一步来。 1)在页面 A(用户表单)中,我从字段中获取一些值并将其作为 jQuery AJAX POST 发送到页面 B。这是主要
我正在尝试弄清楚如何从 buttons.html 获取用户按钮点击动态切换第二个列的可见性 userinput.html 。我正在使用 DataTables userinput.html表并认为我有
我有asp。净 mvc4 项目。 Angularjs 已集成。 我已经按照以前的要求构建了 HTML 页面和 WEB API 2。 现在出于某种原因,我必须使用 CSHTML 页面。以前我只有 web
我的网站包含大约 18 个不同的页面。以管理员身份登录后,我单击“页面”。 我看到了以下内容 All (20) | Mine (19) | Published (19) | Draft (1) No
Haskell 中每个函数都有手册页吗?换句话说,我可以在某处输入 man fst 并获取有关该函数的手册或帮助页面吗?我发现大量的功能势不可挡! 我正在使用 GHC 和 GHCi。 最佳答案 我不知
我一直在用github pages很长一段时间以来,并使用自定义域重定向功能来重定向我的 user website来自 prahladyeri.github.io到我自己的域名 https://www
我对 Github 有一个基本的了解:我知道如何创建、添加、提交、推送和克隆存储库。我也开始探索 Github 页面来托管我的项目。我的最新项目于 3 月开始并将其推送到 gh-page。从那以后,我
在尝试查看日志时,Airflow 会抛出一个带有以下消息的 oops 页面: File "/Users/user/.pyenv/versions/3.5.2/lib/python3.5/locale.
我已经按照此处列出的步骤 (https://pages.github.com/) 为我正在处理的网站设置了一个 GitHub 页面 我设置的站点当前托管在 IIS 下,并使用 URL Rewrite
我想将我的 domain.com 重定向到 openshift 页面 (openshift-app.rhcloud.com)。我已经按照描述完成了 here它有效。唯一的问题是,在任何网站上,我都不会
我是一名优秀的程序员,十分优秀!