Currently, I am facing an issue with switching the template URL in my directive and transferring model data.
I attempted to use a function to switch the template as shown below:
.directive("sqGridData", function ($sce, $rootScope, angularProxySrv) {
return {
...
template: function (tElement, tAttrs) {
//Not working got Exception
//var val = $rootScope.$eval(tAttrs.sqType);
if (tAttrs.sqType) {
if (tAttrs.sqType === 'Iframe') {
return '<div sq-iframe ></div>';
}
if (tAttrs.sqType === 'Table') {
return '<div sq-table></div>';
}
}
},
However, this approach did not work because the value of tAttrs was an expression rather than the actual value itself like "{{item.WidgetConfig.WidgetType}}."
<div style="height: 90%;" sq-grid-data="item.WidgetConfig" sq-type="{{item.WidgetConfig.WidgetType}}"></div>
Next, I tried using ngInclude and switching the template in the link function. The templates in the HTML are the same as above in the template function.
template: '<div ng-include="cntUrl" style="height:100%;"></div>',
link: function (scope, elem, attr) {
var wasCalled = false;
if (!wasCalled) {
wasCalled = true;
if (scope.sqGridData.WidgetType === 'Iframe') {
scope.cntUrl = "/Templates/widget.directives.iFrame.html";
}
if (scope.sqGridData.WidgetType === 'Table') {
scope.cntUrl = "/Templates/widget.directives.table.html";
}
}
}
While this method seems to be effective, I am now facing challenges with passing model values to my directives loaded in the include. I initially thought it could be resolved through "require":
.directive("sqIframe", function ($sce) {
return {
restrict: "A",
require: "^sqGridData",
template: '<div style="height:100%;"></div>',
scope: {
sqIframe: "="
},
link: function (scope, elem, attr, ngModel) {
scope.cntUrl = "Home/WidgetHtmlTemplate?name=" + ngModel.HtmlTemplateName;
scope.IframeUrl = $sce.trustAsResourceUrl(ngModel.WidgetName);;
}
}
}
})
Unfortunately, I encountered an error stating that the controller "sqGridData" could not be found. Is there a more efficient way to address this issue or does anyone have insights on where I might be going wrong?