2

I am trying to use ng-repeat and a controller to dynamically load tab content.

Controller code:

angular.module('ogn.userprefs').controller('UserprefsController', ['$scope',
    function($scope) {
        $scope.tabs = [
            { title: 'Personal Information', content: "ng-include=''modules/userprefs/views/personalinfo.html''"},
            { title: 'Sign In & Security', content: 'modules/userprefs/views/signin_security.html'},
            { title: 'Account Preferences', content: 'modules/userprefs/views/accountprefs.html'}
        ];
}]);

HTML code:

<div ng-show="subnav" class="userprefs">
  <div ng-controller="UserprefsController" class="subnav-tabs">
    <!--<tabset>
      <tab heading="Personal Information">
        <div ng-include="'modules/userprefs/views/personalinfo.html'"></div>
      </tab>
      <tab heading="Sign In &amp; Security">
        <div ng-include="'modules/userprefs/views/signin_security.html'"></div>
      </tab>
      <tab heading="Account Preferences">
        <div ng-include="'modules/userprefs/views/accountprefs.html'"></div>
      </tab>
    </tabset>-->
    <tabset>
      <tab ng-repeat="tab in tabs" heading="{{tab.title}}" active="tab.active">
        {{tab.content}}
      </tab>
    </tabset>
  </div>
</div>

The HTML code that has been commented out is how I called the content in my tab before, using hg-include. I want to switch to an ng-repeat and call it through the controller, but all that is coming up is text that follows content:. What can I replace after content to call my html pages?

Any help is appreciated! Thanks in advance!

2
  • Do you mean you are getting the HTML, but encoded/escaped? Commented Nov 16, 2015 at 22:19
  • @Meligy so the original code (commented out) uses ng-include to call my HTML pages, but when I try to call the same URL in the controller, it just comes back as text and not the page in that tab. Commented Nov 16, 2015 at 22:22

3 Answers 3

1

You should use the <ng-include> directive and set the src attribute to your scope variable. Something like this:

<tab ng-repeat="tab in tabs" heading="{{tab.title}}" active="{{tab.active}}">
    <ng-include src="tab.content"></ng-include>
</tab>

Here's a js fiddle demonstrating the usage.

Sign up to request clarification or add additional context in comments.

Comments

0
angular.module('ogn.userprefs').controller('UserprefsController', ['$scope',
    function($scope) {
        $scope.tabs = [
            { title: 'Personal Information', content: 'personalinfo.html'},
            { title: 'Sign In & Security', content: 'signin_security.html'},
            { title: 'Account Preferences', content: 'accountprefs.html'}
        ];
}]);


<div ng-show="subnav" class="userprefs">
  <div ng-controller="UserprefsController" class="subnav-tabs">
    <tabset>
      <tab ng-repeat="tab in tabs" heading="{{tab.title}}" active="tab.active">
        <div ng-include="modules/userprefs/views/{{tab.content}}"></div>
      </tab>
    </tabset>
  </div>
</div>

This should work, but here is a little plunker i prepared with example. I guess that first index in tabs scope is messing around with double ''. Remember to download and include ui.bootstrap

Comments

0

You should really try and either preload these templates or use custom directives as using <ng-include> creates an unnecessary watch. Also consider using one-time binding syntax for your tab array if you don't expect it to change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.