In this example plunker, http://plnkr.co/edit/k2MGtyFnPwctChihf3M7, the nested directives compile fine when calculating the DOM layout, but error when the directive tries to reference a variable to bind to and says the variable is undefined. Why does this happen? The data model I am using is a single model for many nested directives so I want all nested directives to be able to edit the top level model.
-
Kudos for providing a plunk, but you really need to describe your problem, the errors or issues you're seeing, and the expected result that you don't see. All I see is a bunch of lines.Jason Goemaat– Jason Goemaat2014-04-15 22:41:05 +00:00Commented Apr 15, 2014 at 22:41
-
"but error when the directive tries to reference a variable to bind to and says the variable is undefined" is the error, "data model I am using is a single model for many nested directives so I want all nested directives to be able to edit the top level model" is what I am trying to do. All I'm trying to do is to get the data key in the model to view properly, which you would see if you looked at the plunkerajklein– ajklein2014-04-15 22:44:05 +00:00Commented Apr 15, 2014 at 22:44
-
edit: looked at the plunker and opened up devtoolsajklein– ajklein2014-04-15 22:46:31 +00:00Commented Apr 15, 2014 at 22:46
2 Answers
I havn'et got a clue as to what you're trying to do. However, your comment 'so I want all nested directives to be able to edit the top level model' indicates you want your directive to have scope of your controller. Use
transclude = true
in your directive so that your directives can have access to your the parent scope. http://docs.angularjs.org/guide/directive#creating-a-directive-that-wraps-other-elements
Comments
I don't know why you are doing it this way exactly, it seems like there should be a better way, but here goes a stab at getting your code working. First you create an isolated scope, so the scopes don't inherit or have access to anything but what is passed in the data attribute. Note that you can have your controller set dumbdata = ... and say <div data="dumbdata" and you will only have a data property on your isolated scope with the values from dumbdata from the parent in the data property. I usually try to use different names for the attribute and the data I'm passing to avoid confusion.
app.directive('project', function($compile) {
return {
template: '<div data="data"></div>',
replace: true,
scope: {
data: '=' // problem
},
Next, when you compile you are passing variables as scopes. You need to use real angular scopes. One way is to set scope: true on your directive definition, that will create a new child scope, but it will inherit from the parent.
app.directive('outer', function($compile) {
var r = {
restrict: 'A',
scope: true, // new child scope inherits from parent
compile: function compile(tEle, tAttr) {
A better way is probably to create the new child scope yourself with scope.$new(), and then you can add new child properties to pass for the descendants, avoiding the problem of passing values as scopes and still letting you have access to the individual values you're looping over (plunk):
app.directive('outer', function($compile) {
var r = {
restrict: 'A',
compile: function compile(tEle, tAttr) {
return function postLink(scope,ele,attrs) {
angular.forEach(scope.outer.middles, function(v, i) {
var x = angular.element('<div middle></div>');
var s = scope.$new(); // new child scope
s.middle = v; // value to be used by child directive
var y = $compile(x)(s); // compile using real angular scope
ele.append(y);
});
};
}
};
return r;
});