Javascript 在角度视图中更改外部模板
我正在将一个产品管理管理站点转换为使用Angular-我的所有视图(产品列表、详细视图、编辑等)都显示在我的管理页面上的ng视图中。那里一切都好。然而,我有一个链接,每个产品,让我打印它的信息-它目前使用不同的外部模板作为其他 处理这个问题的角度是什么 应用程序: 在我的管理列表中:Javascript 在角度视图中更改外部模板,javascript,angularjs,Javascript,Angularjs,我正在将一个产品管理管理站点转换为使用Angular-我的所有视图(产品列表、详细视图、编辑等)都显示在我的管理页面上的ng视图中。那里一切都好。然而,我有一个链接,每个产品,让我打印它的信息-它目前使用不同的外部模板作为其他 处理这个问题的角度是什么 应用程序: 在我的管理列表中: <div ng-repeat="order in orders"> <a title="Print product sheet" href="/order/print/{{ order._id
<div ng-repeat="order in orders">
<a title="Print product sheet" href="/order/print/{{ order._id }}"></a>
</div>
现在,这将导致_print.html与另一个放在同一个ng视图中。我确实希望它在一个新窗口中打开—我是否可以创建一个新的应用程序?您可以编写一个服务,并在该服务中执行ajax调用以获取html。现在您的html将包含占位符,即{{},因此您需要一个模板库(例如mustache)来用真实数据替换那些{}
factory('print', ["$window", function($window) {
/* service in charge of printing */
return function(templateUrl, context) {
/* send a GET request to templateUrl for template, render the template with context
* and open the content in a new window.
*/
$.ajax({
url: templateUrl,
async: false, //otherwise the popup will be blocked
success: function(html) {
var template = html.replace('<!DOCTYPE html>\n<html lang="en">\n', '').replace("</html>", ''),
output = Mustache.render(template, context);
$window.open().document.documentElement.innerHTML = output;
}
});
};
工厂('print',[“$window”),函数($window){
/*负责印刷的服务*/
返回函数(templateUrl、上下文){
/*向模板的templateUrl发送GET请求,使用上下文呈现模板
*并在新窗口中打开内容。
*/
$.ajax({
url:templateUrl,
async:false,//否则弹出窗口将被阻止
成功:函数(html){
var template=html.replace('\n\n','').replace(“,''),
输出=Mustache.render(模板、上下文);
$window.open().document.documentElement.innerHTML=输出;
}
});
};
}]))
factory('print', ["$window", function($window) {
/* service in charge of printing */
return function(templateUrl, context) {
/* send a GET request to templateUrl for template, render the template with context
* and open the content in a new window.
*/
$.ajax({
url: templateUrl,
async: false, //otherwise the popup will be blocked
success: function(html) {
var template = html.replace('<!DOCTYPE html>\n<html lang="en">\n', '').replace("</html>", ''),
output = Mustache.render(template, context);
$window.open().document.documentElement.innerHTML = output;
}
});
};