I have a directive which needs to display data from an object in a controller. The piece of data that must be displayed is determined by an attribute of the directive and some other calculations based on other dom elements.
Here is a simplified example.
I have this data:
app.controller('EditorCtrl', function($scope) {
$scope.blocks = {
header: 'text1',
body: 'text2',
};
});
And I want it to be displayed with this directive:
app.directive('editable', function() {
return {
template: 'data: {{val}}',
link: function(scope, element, attrs) {
element.wrap('<div class="editable"></div>');
data = scope.blocks[attrs.editable];
val = data;
}
}
});
And in HTML:
<h1 editable="header">text1 should be displayed here</h1>
<h1 editable="body">text2 should be displayed here</h1>
How can I accomplish this ? Am I approaching the problem in a right way ?