我正在开发一个关于图片帖子的社交应用,我们必须在应用中的多个屏幕上显示这些帖子。问题是,我们在每个屏幕模板中重复帖子的结构,所以如果我们在某个模板中更改帖子的结构,我们必须在每个模板中更改它。
我的问题是,可以创建一个模板 - 在这种情况下是帖子的结构 - 并将post对象作为参数发送,这样这个新模板可以正确加载它吗?当然,在我们需要的地方调用这个模板,比如Django块和模板继承。有没有办法在Angular中做到这一点?谢谢。
答案 0 :(得分:0)
如果我理解你的问题,我认为你需要使用指令
<强>脚本强>
angular.module('docsTemplateUrlDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
templateUrl: 'my-customer.html'
};
});
<强>的index.html 强>
<div ng-controller="Controller">
<div my-customer></div>
</div>
<强>模板强>
Name: Naomi Address: 1600 Amphitheatre
与文档的链接是here
更新1
根据您的问题,您可以传递和反对该指令,因为您需要执行类似以下代码的操作。
myApp.directive('passObject', function() {
return {
restrict: 'E',
scope: { obj: '=' },
template: '<div>Hello, {{obj.prop}}!</div>'
};
});
myApp.controller('MyCtrl', function ($scope) {
$scope.obj = { prop: "world" };
});