So lets say in my HTML I have something like this:
<tabcontent></tabcontent>
Then the javascript for this directive is this:
tabsApp.directive('tabcontent', function(){
var myObj = {
priority:0,
template:'<div></div>',
replace: true,
controller: 'TabCtrl',
transclude: false,
restrict: 'E',
scope: false,
compile: function (element, attrs){
return function (parentScope, instanceEle){
parentScope.$watch('type', function(val) {
element.html('<div '+val+'></div>');
});
}
$compile(parentScope);
},
link: function postLink(scope, iElement, iAttrs){}
};
return myObj;
});
The HTML is parsed properly, and the value for type is found in the controller JS.
so <tabcontent></tabcontent> is replaced with <div recipe></div> for example..
(that part does happen properly)
So I also have a directive for recipe:
tabsApp.directive('recipe', function(){
var myObj = {
priority:0,
template:'<div>TESTING</div>',
replace: true,
controller: 'TabCtrl',
transclude: false,
restrict: 'E',
scope: false,
compile: function (element, attrs){
return {
pre: function preLink(scope, iElement, iAttrs, controller){},
post: function postLink(scope, iElement, iAttrs, controller){}
}
},
link: function postLink(scope, iElement, iAttrs){}
};
return myObj;
});
Which is obviously pretty simple, and just for testing. But the recipe directive is not being processed...
Whats going on here?
... is quite illustrative as AngularJS doesn't allow multiple directives (on the same DOM level) to create their own isolate scopes. According to the documentation, this restriction is imposed in order to prevent collision or unsupported configuration of the $scope objects.
The ngRef attribute tells AngularJS to assign the controller of a component (or a directive) to the given property in the current scope. It is also possible to add the jqlite-wrapped DOM element to the scope. The ngRepeat directive instantiates a template once per item from a collection.
The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
Two-way Binding Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well.
You need to change 2 things:
The recipe
directive must not be restricted to E (element). If you are generating the directive like <div recipe></div>
, you must at least add A (attribute) to the restrict
property on the directive configuration:
app.directive('recipe', function() {
return {
restrict: 'E',
...
You need to compile the HTML content of the tabcontent
directive after the 'watch':
app.directive('tabcontent', function($compile){
return {
...
link: function (scope, iElement, iAttrs) {
scope.$watch('type', function(val) {
iElement.html('<div '+val+'></div>');
$compile(iElement.contents())(scope);
});
}
...
jsFiddle: http://jsfiddle.net/bmleite/n2BXp/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With