使用angular JS以编程方式将模板绑定到模型
Bind template to a model programmatically using angular JS
给定一个HTML模板
<div class="info">
<div class="title"><a href="property-detail.html">{{title}}</a></div>
<div class="location">{{location}}</div>
<div class="property-info clearfix">
<div class="area"><i class="icon icon-normal-cursor-scale-up"></i>{{size}}<sup>2</sup></div>
<div class="bedrooms"><i class="icon icon-normal-bed"></i>{{bedrooms}}</div>
<div class="bathrooms"><i class="icon icon-normal-shower"></i>{{bathrooms}}</div>
</div><div class="price">{{price}}</div>
<div class="link">
<a href="{{details}}">View more</a>
</div>
</div>
以及一个适合这个模板的模型(也就是说,拥有所有字段:title、location、price等)。我正在寻找一种方法,以编程方式将模板绑定到模型,然后将渲染结果推送到数组中。在伪代码中,我想做一些类似的事情:
var boxes= [];
for (var i = 0; i < items.length; i++) {
var results = bind(template, items[i]);
boxes.push(results);
}
Where items是我从数据库或任何其他来源获得的一个项数组,bind基本上是负责用模型填充模板的函数。
使用指令是有意义的。但不知道该怎么做。
知道是否可以使用Angular以及如何使用Angular吗?
通常情况下,您永远不希望在指令之外的任何操作中操作DOM。但为什么要对它教条化呢?如果你真的需要一个字符串格式的编译模板(用于第三方小部件消费等),那么你可以组合一个类似于以下的服务:
angular.module('app', []);
angular.module('app').controller('MainCtrl', function ($scope, templateCompiler) {
var boxes = [];
var data = [{
title: 'test',
location: 'location!',
size: 40,
bedrooms: 'yes',
bathrooms: 'uncertain',
price: 'if you have to ask...',
details: 'indeterminate'
},{
title: 'test2',
location: 'somewhere',
size: 'a woman never tells',
bedrooms: 3.14,
bathrooms: null,
price: 1400,
details: 'forthcoming'
}];
for (var i = 0; i < data.length; i++) {
var results = templateCompiler.bind(data[i]);
boxes.push(results);
}
$scope.boxes = boxes;
})
angular.module('app').service('templateCompiler', function ($compile, $templateCache, $rootScope) {
var service = {}
var template = $templateCache.get('boxTemplate');
var scope;
this.bind = function (data) {
scope = $rootScope.$new();
angular.extend(scope, data);
var link = $compile(template);
var content = link(scope);
scope.$apply();
return content.html();
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app='app'>
<script type="text/ng-template" id="boxTemplate">
<div class="info">
<div class="title"><a href="property-detail.html">{{title}}</a></div>
<div class="location">{{location}}</div>
<div class="property-info clearfix">
<div class="area"><i class="icon icon-normal-cursor-scale-up"></i>{{size}}<sup>2</sup></div>
<div class="bedrooms"><i class="icon icon-normal-bed"></i>{{bedrooms}}</div>
<div class="bathrooms"><i class="icon icon-normal-shower"></i>{{bathrooms}}</div>
</div><div class="price">{{price}}</div>
<div class="link">
<a href="{{details}}">View more</a>
</div>
</div></script>
<!-- TO PROVE YOU GET THE DESIRED OUTPUT -->
<div ng-controller="MainCtrl">{{boxes}}</div>
</div>
您还可以考虑将地图小部件封装在指令中。
相关文章:
- 在VanillaJS中模拟模型双向数据绑定
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- Backbone.js将模型绑定到视图时出错
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- 当显式定义控制器参数时,默认模型绑定器会发生异常
- Ember.js:通过绑定/模型查找DOM元素
- AngularJS绑定模型以选择更新输入数字字段,最小值为最大值
- 淘汰MVC,绑定模型&将对象添加到模型中
- 从ajax中为传递为null的日期值绑定模型
- Ember.js 不会自动绑定模型 REST 更改
- 骨干JS,绑定模型查看
- 如何在角度指令中绑定模型
- Ember.js绑定模型存储在数组中
- 在javascript函数中绑定模型并将其传递给控制器
- 当浏览器填充表单时,AngularJS没有绑定模型
- Angularjs:当模型改变时,单选按钮检查状态绑定模型值失败
- 绑定模型在EmberJS中变为视图
- angularjs - ng-file-upload不绑定模型到动态创建的HTML表单
- 剑道自动完成不会绑定模型
- Emberjs:正确绑定模型/控制器/视图与夹具数据