javascript - 为什么我在新打开的窗口中看不到字形图标?

标签 javascript html angularjs twitter-bootstrap

我在 $window.open 命令的帮助下打开新窗口并将 html 内容写入窗口。

这是 Javascript 代码:

var test = angular.module('test', []);

test.controller('testController', ['$compile', '$scope','$window', function($compile, $scope, $window) {
    $scope.openWindow = function() {
       $window.open('', '_blank', 'width=500,height=400').document.write($("#report").html());
    };}]);

这是我写到新窗口的内容:

<div ng-app="test" id = "report" ng-controller="testController">
 <p>Envelope icon: <span class="glyphicon glyphicon-envelope"></span></p>
<p>Search icon: <span class="glyphicon glyphicon-search"></span></p>
<p>Print icon: <span class="glyphicon glyphicon-print"></span></p>
<hr/>
    <button ng-click="openWindow()">push!</button>
</div>

这里正在工作fiddle .

问题是我在新打开的窗口中看不到字形图标。

知道为什么我看不到字形图标吗?

最佳答案

您的代码中有两个错误。首先,您没有正确包含 css。您应该使用 href 属性而不是 rel 属性来包含 css。其次,您必须使用 integrity 键检查您是否包含来自外部的 Bootstrap 。

正确的包含是这个:

var test = angular.module('test', []);

test.controller('testController', ['$compile', '$scope','$window', function($compile, $scope, $window) {
    $scope.openWindow = function() {
    var content = $("#report").html();

    var html = '<html><head><title>hi</title><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"></head><body>'+content+'</span></p></body></html>'   

       $window.open('', '_blank', 'width=500,height=400').document.write(html);                         
    };}]);

更新后的 fiddle :http://jsfiddle.net/d8atdw0t/283/

如果你想从你的项目中引用样式表,你可以加载一个带有查询参数的页面模板,比如 http://yoursite.com/page.html?window=true,然后在 $window 加载时,您使用查询参数指定 url。

$window.open('http://yoursite.com/page.html?window=true', '_blank', 'width=500,height=400');

根据您加载模板文件的方式(例如从后端加载),如果查询参数存在,您可以限制加载模板文件。

关于javascript - 为什么我在新打开的窗口中看不到字形图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37806040/

相关文章:

javascript - HTML/CSS/Javascript 可滚动的 div 不会滚动?

javascript - 完整日历点击事件谷歌日历的时间错误

javascript - 配置 Angular UI 按钮

html - 使位置固定不可滚动

javascript - AngularJS 在 1 个模板中使用一些 Controller

javascript - OnsenUi getCurrentPage() 给出空对象

php - SQL:如何在sql中实现特定表的多条数据的插入/更新

javascript - 我可以用 ng-if 调用函数吗?

javascript - AngularJS 包含一个位于文件夹中的文件

javascript - 隐藏客户端 API 调用