Goal
Use ng-repeat to create multiple directive instances.
Problem
{{box}} isn't evaluating like I expected in the following code:
<div ng-controller='ClickboxCtrl' ng-repeat="box in lmfOptionset.boxes">
<div lmf-clickbox='{{box}}' class='lmf-clickbox'></div>
</div>
Relevent Code (I think.)
ClickboxCtrl
.controller('ClickboxCtrl', ['$scope', function($scope){
$scope.traditional = {
title : 'Traditional',
description : 'This is the description',
background_img : ''
};
$scope.cross_slot = {
title : 'Cross Slot',
description : 'This too.',
background_img : ''
};
}])
OptionsetCtrl
.controller('OptionsetCtrl', ['$scope', function($scope){
$scope.traditional_or_cross_slot = {
boxes : ['traditional', 'cross_slot'],
header: 'Would you prefer?'
};
lmfOptionset
.directive('lmfOptionset', function() {
return {
scope: {
'lmfOptionset' : '='
},
link: function(scope){
},
templateUrl: 'option_set/option_set.html'
}
})
lmfClickbox
.directive('lmfClickbox', ['State', function(State) {
return {
scope: {
lmfClickbox: '@'
},
link: function($scope, element, attrs){
element.on('click', function(){
//When a clickbox is clicked, add it to the state.
State.add_option(attrs.lmfClickbox);
});
},
templateUrl: 'clickbox/clickbox.html'
};
}])
What I've Tried
I have only been using Angular for about four days now, so I am still learning a lot about it. It has lots of new terms (transclusion, directive, etc.) to get used to. However, my research has led me to many ideas.
Initially, I thought it was a scope problem and that I needed to look at the way I was using isolate scopes ('=', '&', '@') but my combinations of these didn't lead anywhere.
After that, I realized that after refreshing the page, I was getting the following code:
<div lmf-clickbox="{{box}}" class="lmf-clickbox ng-isolate-scope"><div class="lmf-title ng-binding"></div>
So I thought, maybe it's a timing issue? So I tried adding some code to evaluate it using code from this stackoverflow question but I didn't find any luck there.
Finally I've tried adding transclusion: true to my directive because, well, I just needed to try something else.
Any help would be appreciated with this problem!
lmfOptionsetis not declared/set anywhere in the code you provided. Also I would recommend not putting theng-repeaton the same element as theng-controller. I don't know for sure if this works or not works, it just feels wrong.lmfOptionsetdirective but now i'm even more confused than before.ng-repeatneeds to refer to a property set on$scope, so you would need$scope.lmfOptionset=somewhere insideClickboxCtrllmfOptionsetis its own directive, so it has its own isolated scope. You're not actually using this directive anywhere, so it's never instantiated and the scope variable is never set anywhere. I'm sorry to say I think you're really far from the solution. Can you take a step back from angular, and describe (irrespective of angular) what you want to do? i.e., what data do you have, and what html do you want to generate, and what parts do you want to abstract out to be reusable?