As the title states, I am trying to compile, and link an angular controller, to dynamic HTML.
My goal is to create a service, that takes a set of options, one of which would be a controller, that I could link to the dynamic HTML before appending it to the DOM, which would then give the user of this service acces to that bound controller's $scope as well, with the ability to optionally override the scope, should they so wish.
I've attempted this with the $controller and $compile services, but for some reason the template is being appended, but is not being processed, it does not seem like scope is being set, as once appended, it displays the template tags, as opposed to the actual values.
Should I be using either $controller or $compile, but not in conjunction?
I'm not entirely sure where I am going wrong, some guidance would be much appreciated.
panel_template.html
<div class="pathwindow">
<div class="pathwindow_title">
<h1>{{ title }}</h1>
</div>
<div class="pathwindow_content">
{{ content }}
</div>
</div>
app.controller.js
(function () {
'use strict';
var appController = angular.module('app.controller', []);
appController.controller('appController', ['$state', '$scope', '$rootScope', 'PanelService', function ($state, $scope, $rootScope, PanelService) {
$scope.title = 'A Awesome Title';
$scope.content = 'This is some content...';
$scope.clickMe = function () {
PanelService.show('panel_template.html', 'appController');
};
}]);
})();
app.factory.js
(function () {
'use strict';
var appFactory = angular.module('app.factory', []);
appFactory.factory('PanelService', ['$injector', '$controller', '$compile', '$rootScope', '$http', function ($injector, $controller, $compile, $rootScope, $http) {
var self = {};
self.show = function (template_url, controller, scope) {
var $scope = angular.isObject(scope) ? scope.$new() : $rootScope.$new();
$http.get(template_url).then(function(response){
var html = response.data;
angular.element('#panels').append(html);
$controller(controller, { $scope: $scope, $element: html });
$compile(angular.element(html))($scope);
});
};
return self;
}]);
})();