在Angular中修改外部模板
Changing outer templates in Angular
我正在将一个产品管理管理网站转换为使用Angular——我的所有视图(产品列表、详细信息视图、编辑视图等)都显示在我的管理页面的ng视图中。一切都好。然而,我有一个链接,每个产品,让我打印它的信息-它目前使用不同的外部模板作为其他的。
处理这个问题的角度方法是什么?
应用程序:
angular.module('myApp', []).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
// all routes but print use an index.html template
$routeProvider.
when('/', {
templateUrl: '/partials/order/manage.html',
controller: ManageOrderCtrl
}).
when('/order/:id', {
templateUrl: '/partials/order/_view.html',
controller: ViewOrderCtrl
}).
when('/order/edit/:id', {
templateUrl: '/partials/order/_edit.html',
controller: ViewOrderCtrl
}).
// I want this link to not use the same outer template (i.e. something like printer.html as well as use custom headers)
when('/order/print/:id', {
templateUrl: '/partials/order/_print.html',
controller: PrintOrderCtrl
}).
otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
}]);
管理列表:
<div ng-repeat="order in orders">
<a title="Print product sheet" href="/order/print/{{ order._id }}"></a>
</div>
现在这将导致_print.html被放置在同一个ng-view中。我确实想让它在一个新窗口中打开-我只是做一个新的应用程序吗?
您可以编写一个服务,并在该服务内部做一个ajax调用来获取html。现在你的html将包含占位符,例如{{}},所以你需要一个模板库(例如:{{}}替换为真实的数据
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;
}
});
};
}]);
相关文章:
- 在外部声明的变量和XMLHttpRequest中修改的变量的值在此Ajax请求之外不可见
- 使用load()加载外部文件,然后使用fancybox插件进行修改并显示
- 修改函数以从类外部访问私有变量
- 使ng-show/ng-hide与由Angular之外的外部源修改的localStorage一起工作
- Polymer:当数组中的对象被外部代码修改时,更新dom重复元素
- JavaScript - 是否可以修改第三方脚本(外部托管)使用的 setInterval/setTimeout 的行为
- 从AJAX函数中修改外部变量
- Jquery:如何从内部到外部修改项
- 从forEach回调中修改外部数组变量
- 防止HTML被外部代理或蜂窝提供程序修改
- 从外部站点修改html的Javascript
- 保护封闭内的对象不被从外部修改
- 不能在循环内部修改在循环外部声明的数组
- 分组ng-repeat和修改DOM外部指令
- 在Angular中修改外部模板
- 是否可以在应用外部修改本地存储值
- 节点JS可以在for循环期间通过外部函数修改数组
- 如何修改由外部 JavaScript 创建的元素上的类
- 文件上传:如何上传外部云文件夹中的文件,javascript修改
- 我可以修改外部URL的HTML DOM使用窗口打开