I'm trying to create a directive to cut down on the boilerplate code that I'd have to write.
I'm using the angular xeditable directive to allow for inline editing, but when I add the xeditable directive attributes from my directive, it doesn't work.
When I say it doesn't work, I mean usually when I click on the element there is an input box that appears, and right now nothing happens when I click on the element.
Glenn.directive('edit', function() {
return {
restrict: 'A',
template: '{{ content.' + 'ParamData' + '.data }}',
scope: {
ParamData: '@edit'
},
link: function(scope, element, attrs) {
element.attr('editable-text', 'content.' + attrs.edit + '.data');
element.attr('onbeforesave', 'update($data, content.' + attrs.edit +'.id');
}
}
});
So, my first problem is that the xeditable directive doesn't work because it's inside of mine. I'm new to creating angularjs directives, but am wondering if it could have something to do with the way its being compiled?
My second problem is with the template. If my template just looks like this
template: '{{ ParamData }}'
Then it outputs the correct data, but I can't make it work without the other pieces to reference the scope data.
Also, here is what the view looks like using the directive
<h2 edit="portrait_description_title"></h2>
And here is what it would look like if I didn't use a directive to cut down on the boiler code
<h1 editable-text="content.portrait_description_title.data" onbeforesave="update($data, content.portrait_description_title.id)">
{{ content.portrait_description_title.data }}
</h1>
Thanks for any advice!
You have to recompile the element after adding those attributes, here is an example:
Example plunker: http://plnkr.co/edit/00Lb4A9rVSZuZjkNyn2o?p=preview
Things to consider:
content.portrait_description_title.data
in the first place.template:
also accept function, this way you can get theedit
attribute value to construct the template.terminal
directive and raisepriority
, so that at first run, only this diective (out of others in the same element) will get compiled.attrs.$set()
is useful for adding/removing attributes, use it to add theeditable-text
directive andonbeforesave
.edit
attribute, to prevent a recursion after a next compilation.$compile
service to recompile the element, in order to makeeditable-text
andonbeforesave
works.Hope this helps.
Just add another directive inside template of first directive and bind it to scope model that you get from attr. You can also add controller function, and create more models, or logic and bind to directive template.
Also maybe your attributes may not be available on template, than you need to add $watch to your isolated scope model and update another scope model inside controller. That second one model needs to be binded to template. More information about directives you can find on AngularJS docs, but also here is one good article, it can help you:
http://www.sitepoint.com/practical-guide-angularjs-directives-part-two/