Here is the html:
<body ng-app="myCatApp">
<div ng-controller="catagoryController">
<add-remove-lister list="catagories"></add-remove-lister>
</div>
</body>
Here is the JS:
app.controller('catagoryController', ['catagoryList', '$scope', function(catagoryList, $scope) {
$scope.catagories = catagoryList.catagoryList;
}]);
app.directive('addRemoveLister', [function () {
return {
scope: {
list: '='
},
template: function(tElement, tAttrs, scope) {
templateHTML = '<ul>';
var list = scope.list;
for (o = 0; o < list.length; o++) {
var curObject = scope.list[o];
templateHTML +='<li ng-repeat="listItem in list"><button type="button" ng-hide="listItem.userSelected" ng-click="addToList()">Add</button>';
templateHTML +='<button type="button" ng-hide="listItem.userSelected" ng-click="removeFromList()">Remove</button>{{listItem.text}}';
for (var prop in curObject) {
if (curObject.hasOwnProperty(prop) && prop.constructor === Array) {
templateHTML += '<add-remove-lister list="listItem.'+ prop +'"></add-remove-lister>';
}
}
templateHTML += '</li>';
}
templateHTML += '<ul>';
return templateHTML;
}
}
}]);
The code is not working correctly. When I set a breakpoint in the directive, I can see that list is just the string "catagories". I would like it to be the categories object that is on the controller scope...
Let me expand a little bit on what I am trying to do:
I am trying to build a directive that will take any array and produce a list from it. The assumptions are: 1) That all elements in the array will be objects having at least the properties {text : 'text', userSelected: 'bool'}
When the directive encounters an object in the list that has a property with that is itself an array (which is also assumed to contain objects with the above two properties), it needs to recursively call itself on that array.
The directive also needs to display buttons next to each list item to allow the user to change the userSelected property on the object (thereby adding it to the users "options")