I have a popup in which I want to display a template based on what the user selects.
<misc-Modal visible="showMiscModal" template="{{selectedTemplate}}">
Here is an example template (CustomerContact.html):
<div class="modal fade">
<div class="modal-dialog my-order-grid-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<span>this is the Customer Contact template</span>
</div>
</div>
</div>
Here is one of the functions that triggers the modal:
$scope.showCustomerContact = function() {
alert("showing customer contact");
$scope.selectedTemplate = "/desktopmodules/mvc/TechSheetMaint/AngularTemplates/CustomerContact.html";
$rootScope.showMiscModal = true;
};
I'm trying to do this through a directive:
angular.module("aps").directive("pmodal",
function() {
return {
restrict: "E",
transclude: true,
replace: true,
scope: true,
templateUrl: scope.selectedTemplate,
link: function postLink(scope, element, attrs) {
scope.$watch(attrs.visible,
function(value) {
if (value === true)
$(element).modal("show");
else
$(element).modal("hide");
});
$(element).on("shown.bs.modal",
function() {
scope.$apply(function() {
scope.$parent[attrs.visible] = true;
});
});
$(element).on("hidden.bs.modal",
function() {
scope.$apply(function() {
scope.$parent[attrs.visible] = false;
$scope.showPdfModal=false;
});
});
}
};
});
I either get
ReferenceError: scope is not defined
or
Error: [$http:badreq] http://errors.angularjs.org/1.7.0/$http/badreq?p0=undefined" when the page gets loaded.
I also tried the suggestions in this post: Angular.js directive dynamic templateURL
<misc-Modal visible="$root.showMiscModal" template-url="selectedTemplate">
angular.module("aps").directive("miscModal",
function() {
return {
restrict: "E",
templateUrl: function(elem, attrs) {
return attrs.templateUrl || "some/path/default.html";
},
link: function postLink(scope, element, attrs) {
scope.$watch(attrs.visible,
function(value) {
if (value === true)
$(element).modal("show");
else
$(element).modal("hide");
});
$(element).on("shown.bs.modal",
function() {
scope.$apply(function() {
scope.$parent[attrs.visible] = true;
});
});
$(element).on("hidden.bs.modal",
function() {
scope.$apply(function() {
scope.$parent[attrs.visible] = false;
$rootScope.showMiscModal = false;
});
});
}
};
});
but I just end up with this error on page load:
Error: [$templateRequest:tpload] http://errors.angularjs.org/1.7.0/$templateRequest/tpload?p0=%7B%7BselectedTemplate%7D%7D&p1=404&p2=Not%20Found
How do I dynamically change the templateUrl
depending on what template I want to display? I've already looked at the other examples and they don't seem to be working for me.