I'd like to use ng-include in the content of a dynamically generated tab using AngularJs and UI Bootstrap.
I have a Plunker here: http://plnkr.co/edit/2mpbovsu2eDrUdu8t7SM?p=preview
<div id="mainCntr" style="padding: 20px;">
<uib-tabset>
<uib-tab ng-repeat="tab in tabs" active="tab.active" disable="tab.disabled">
<uib-tab-heading>
{{tab.title}} <i class="glyphicon glyphicon-remove-sign" ng-click="removeTab($index)"></i>
</uib-tab-heading>
{{tab.content}}
</uib-tab>
</uib-tabset>
</div>
JS Code:
$scope.addTab = function() {
var len = $scope.tabs.length + 1;
var numLbl = '' + ((len > 9) ? '' : '0') + String(len);
var mrkUp = '<div>' +
'<h1>New Tab ' + numLbl + ' {{foo}}</h1>' +
'<div ng-include="tab.tabUrl" class="ng-scope"></div>' +
'</div>';
$scope.tabs.push({title: 'Tab ' + numLbl, content: $compile(angular.element(mrkUp))($scope)});
}
In the Plunker, click the "Add Tab" button. It calls a function in $scope that pushes a new tab to the collection but passing in some dynamically generated content that includes a ng-include directive. The expected output is that the ng-include will be displayed inside of the tab content area.
Thanks
In your Plunker you are using
ng-bind-html
which doesn't compile the HTML for you. You can create a new directive that does that for you.Source code for
ng-bind-html
:Pick a name for the new directive, for example
compile-html
.Replace
tAttrs.ngBindHtml
withtAttrs.compileHtml
(or whatever name you picked).You need to replace
$sce.getTrustedHtml
with$sce.trustAsHtml
, or you will getError: [$sce:unsafe] Attempting to use an unsafe value in a safe context.
Then you need to call
$compile
:Full directive:
Usage:
Demo: http://plnkr.co/edit/TRYAaxeEPMTAay6rqEXp?p=preview
My situation might not be as complex, so this simple solution works:
HTML is
In my case the first tab is populated by the Angular router, which is why the tab array is one index out from
tabs.active