javascript - Pinterest 板仅在页面重新加载时有效

标签 javascript jquery angularjs pinterest

我有一个 AngularJS 应用程序,它是一个单页应用程序。在每个单独的页面上都会加载 Pinterest 板,这是通过 Pinterest 的小部件构建器完成的:

This can be found here

它为我提供了以下内容以粘贴到我的应用程序中:

<a data-pin-do="embedBoard" data-pin-board-width="400" data-pin-scale-height="240" data-pin-scale-width="80" href="https://www.pinterest.com/pinterest/pinstudio/"></a>

和:

<script async defer src="//assets.pinterest.com/js/pinit.js"></script>

我添加了这个并加载了 Pinterest 板,但是当转到不同的页面时,如果没有页面刷新,Pinterest 板就不会加载,我相信这是因为脚本无法重新生成/或加载特定的页面上的元素,它只是不知道它在那里。

因此,我在顶级 Controller 中的 Angular Js 应用程序中使用了以下内容:(从 here 获取此内容)

$scope.loadScript = function(url, type, charset) {
    if (type===undefined) type = 'text/javascript';
    if (url) {
        var script = document.querySelector("script[src*='"+url+"']");
        if (!script) {
            var heads = document.getElementsByTagName("head");
            if (heads && heads.length) {
                var head = heads[0];
                if (head) {
                    script = document.createElement('script');
                    script.setAttribute('src', url);
                    script.setAttribute('type', type);
                    if (charset) script.setAttribute('charset', charset);
                    head.appendChild(script);
                }
            }
        }
        return script;
    }
}; 

然后,我将以下内容插入到我的子 Controller 中以加载脚本:

$scope.$parent.loadScript('//assets.pinterest.com/js/pinit.js');

这可以正常工作并将我的脚本添加到页面中,但是 pinterest 板在刷新之前不会加载,我没有想法,我似乎无法找出为什么脚本在页面刷新之前不会加载元素.

任何帮助都会很棒。

最佳答案

可能是因为加载脚本时embedBoard元素不存在。

在类似的上下文中,为了在 Angular 应用程序上制作 Pin 按钮,我们使用 PinUtils.pinAny();

对于主板,您可以尝试angular-pinterest .

关于javascript - Pinterest 板仅在页面重新加载时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41714725/

相关文章:

javascript - 如何从循环数组索引 i 和 j 返回新数组

javascript - 有没有办法自动向下滚动并加载任何使用无限滚动的网站的更多内容?

javascript - Requirejs 无法编译我的 dojo 依赖模块

javascript - Angular 1 组件路由器 : Cannot read property 'startsWith' of undefined

过滤器内的 Javascript 映射

jquery - 为什么使用 jQuery 来获取 CSS 资源?

javascript - 试图设置动态添加输入的焦点

jquery - 更改子元素悬停时的父元素背景位置

javascript - AngularJS Controller 在第一次刷新页面后不起作用

javascript - $http 获取逻辑只允许 200 OK