There are many ways to wrap, combine, augment and re-use directives. But the best way depends on your actual use-case and what you are trying to accomplish. In your case, I would argue wrapping an element within-element is not the most semantic way of doing it.
I created a new example based on yours that just focuses on decorating the my-form
directive by appending a submit button on the end:
http://plnkr.co/edit/SFIPXwLnozSY6fLOOOyU?p=preview
The syntax for using this “decorator” would be:
<my-form with-submit-button values="values.form"></my-form>
This is more semantically correct than if you were to do the same “decorator” by wrapping:
<with-submit-button>
<my-form values="values.form"></myForm>
</with-submit-button>
Here is the code for the ‘with-submit-button’ directive:
module.directive('withSubmitButton', ['$compile', function($compile) {
return {
restrict: 'A',
compile: function() {
var submitButton = $compile('<button ng-click="submit()">Submit</button>');
return function link(scope, element) {
submitButton(scope, function(dom){
element.append(dom);
})
scope.submit = function() {
console.log('Submitting ' + JSON.stringify(scope.values));
};
};
}
};
}]);
Notice how you can’t use template:
because only one directive is allowed to use template. Instead we inject $compile:
and use it to compile are little button template, we then link this button to our scope and append it to our element.
This isn’t the only way to do this, but like I said, there is no “one way” and the best way will depend a lot on your actual use case.