Need a bit of help to get this to finished it off. But if someone has a better suggestion I am an open minded individual.
Basically I've written a directive to swap out templates based on directive attribute.
<div id="popup" class="popup my-directive" type="player" style="display: none;"></div>
Problem that I have is changing the type attribute such that it would trigger $digest and re-evaluate my directive.
$scope.selectTeam = function () {
$scope.type = 'team';
//$scope.$apply(function () {
// bind directive
$(".popup").attr("type", "team");
// show popup div
$(".popup").show();
//});
};
The purpose of type attribute is to track which template will be bound to popup div.
app.directive('myDirective', function () {
return {
restrict: 'AEC', // Attribute, Element, Class
transclude: true, // expose controller scope
templateUrl: function(element, attr){
var type = typeof attr.type !== 'undefined' ? attr.type : 'player';
if(!type) return;
return 'search-' + type + '-tpl.html';
},
}
});
For live example please have a look at my plunker.
I will now try something with $compile to build my popup with new type value in each select function but really don't fancy this approach.
update
OK, I have cracked it quicker than originally expected. Live plunker.
Changes I introduced to controller:
$scope.selectTeam = function () {
$scope.type = 'team'; // trigers $digest
$(".popup").replaceWith($compile(angular.element(document).find("my-directive").attr("type", "team"))($scope));
$(".popup").show();
};
Changes I introduced to HTML:
<my-directive id="popup" class="popup" type="player" style="display: none;"></my-directive>
The only issue this present that I noticed is Cannot read property 'insertBefore' of null when I try to search. But I can live with this.
As I mentioned before if someone has a different idea/approach or noticed something funny in my code please do share.
something. Depending on what thissomethingis the search and the values may differ. Thats why I went with a directive after I scrapped my$compileversion - plnkr.co/edit/JKOYHMdaarUrl4R11dKm?p=preview. While I have used extremely simple examples in my plunkers in reality these search/select pop-ups will be more complicated. I hope this gives a bit of an insight.