Have a bit of an interesting issue (at least I think so:) )
Changed my app to use a factory instead of having everything in the controller, all considered good practice (or so I have heard), the factory takes care of a bunch of data, all kinds really, and different functions handles different calls.
The one I am having a problem with is one that generates a bunch of buttons based on data received from a database. A full version of it would not make much sense, but below is a proof of concept just adding a hello button.
learnByPlay.factory('padArea', function($window, $http, $q, $compile){
factory.loadButtons = function() {
var newButton = '<button class="padbtn" ';
newButton = newButton + 'data-ng-click="sayHello(\"Angular\")" ';
newButton = newButton + '<br/>'+Hello +'</button>';
console.log(newButton);
var padElement = $compile(newButton); //This should be the compiled button
console.log(padElement);
$('#newButton').html(padElement);
}
Here is my problem, the compiler requires the $scope, which is not accessible in the factory, but I would rather want to avoid generating the code then compiling it in the controller, as the controller does not know how many buttons or where they are going, that data is all in the factory.
========= UPDATE =========
Please note: I do not believe that ng-repeat is an option in this case as the data generated requires compiling (ng-click can not be included in a ng-repeat directly as ng-repeat will not compile new DOM object for ng-click events, that is my understanding at least).
========= UPDATE 2 =======
This is not a question about how you could use ng-repeat, I know that it is an awesome tool, and that it can do similar thing, but in this case it will not work. As far as I can tell it has to do with that ng-repeat does not compile dom objects, or potentially can not do so dynamically (I do not know enough about the compiler to tell why that is, my theory is that ng-repeat just side steps it, if anyone knows I would be happy to know more, or if I am wrong, please tell me)
For more reading:
ng-click not working from dynamically generated HTML
AngularJS + JQuery : How to get dynamic content working in angularjs
So please, and I say this with the greatest level of respect, do not provide me with yet a solution on how to run ng-repeat.
===============================
Any Ideas?