[英]Open a web page from a Javascript
I am creating a simple web page using angular where a user can type HTML codes in a text box and when I press button it should be open in a new tab or new window. 我正在使用angular创建一个简单的网页,用户可以在其中在文本框中键入HTML代码,当我按下按钮时,它应该在新标签页或新窗口中打开。 I saved the html content in a variable in $scope.
我将html内容保存在$ scope中的变量中。 But I have no idea how to load that in to a new window.
但是我不知道如何将其加载到新窗口中。 Can someone direct me?
有人可以指导我吗?
I don't know if there's an Angular-specific answer here, but if you have the HTML in a string, you can do this (in response to a user action, like a button click) to open a new window/tab and write the HTML to it: 我不知道这里是否有特定于Angular的答案,但是如果字符串中包含HTML,则可以执行此操作(以响应用户操作,例如单击按钮)来打开新窗口/标签并编写它的HTML:
var wnd = window.open();
wnd.document.write(yourHTMLString);
Here's a complete example (without Angular): Live Copy 这是一个完整的示例(无Angular): 实时复制
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>New Window with HTML Example</title>
</head>
<body>
<textarea id="theHTML" cols="50" rows="10">
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Popup</title>
</head>
<body>
<p>Hi there!</p>
</body>
</html>
</textarea>
<br><input type="button" id="theButton" value="Show">
<script>
(function() {
"use strict";
document.getElementById("theButton").onclick = function() {
var html = document.getElementById("theHTML").value;
var wnd = window.open();
wnd.document.write(html);
}
})();
</script>
</body>
</html>
To complete TJ answer, something like this should work : 要完成TJ答案,类似这样的方法应该起作用:
function MainCtrl($scope, $window) {
$scope.createHTML = function(html) {
console.log(html)
var newWindow = $window.open();
newWindow.document.writeln(html);
newWindow.document.close();
}
}
the html: HTML:
<body ng-app ng-controller="MainCtrl">
<form name="html-form" ng-submit="createHTML(html)" class="form" role="form">
<div class="form-group">
<label for="">HTML</label>
<textarea ng-model="html" cols="30" rows="10" class="form-control"></textarea>
</div>
<div class="form-group">
<input type="submit" value="Create HTML" class="btn btn-default" />
</div>
</form>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.