定义Angular模板的更简洁的方式
Cleaner way for defining Angular templates
考虑以下代码
my_app.run( [ '$templateCache' , function( $templateCache ) {
var template = "" +
"{{ item.name }}" +
"<ul ng-if='item.sub'>" +
"<li ng-repeat='item in item.sub' ng-include='"'angular-multi-select-item.htm''"></li>" +
"</ul>" +
"";
$templateCache.put( 'angular-multi-select-item.htm' , template );
}]);
虽然这工作如预期,我认为它相当丑陋(与html内部连接字符串)。有没有其他更干净的方式来定义Angular模板的html ?
你怎么看?在你的工作目录下创建一个viewhtml视图文件,并使用$http
加载。
$templateCache.put('angular-multi-select-item.htm', $http.get('views/template.html'));
您也可以尝试使用' ng-include?(文档)并在主视图中添加include,如:
<div ng-include="'views/template.html'"></div>
相关文章:
- 为什么这在IE中的工作方式与在Firefox中不同
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 使用javascript存储变量的最安全方式
- 以可优化的方式使用requirejs加载模板
- 在Highcharts中,我们可以通过任何方式在渲染图表之前获得plotWidth和plotHeight
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- 为react组件传递道具的最佳方式
- 让Webpack管理Quirky AMD定义的最佳方式
- 链接承诺和传递价值的简洁方式
- JavaScript if语句的简洁方式
- 运行多事件功能的更简洁的方式
- 当下拉菜单中的项目发生变化时,以简洁的方式突出显示标签
- 写一个coffeescript函数,将json数组转换成我想要的格式,这是一种简洁的方式
- 以更简洁的方式声明javascript静态方法
- 简洁的方式获取所有输入字段的值
- 将事件附加到动态呈现字段的更简洁的方式
- 使用JQuery和Angular2进行多个ajax调用的简洁方式
- 定义Angular模板的更简洁的方式