ng-include with a variable for src
Asked Answered
M

2

29

I would like to change the ng-includes src with a variable. Currently this is what I have.

 <div class='alignRightCenter' id='rightContent'>
    <ng-include src="'{{view}}'"> </ng-include>
</div>

And here is the controller:

ctrl.controller('contentCtrl', ['$scope', function($scope) {
      $scope.view = "partials/setListName.tpl.html";
  }]);

Unfortunately I can not use a ng-view because it is already being used to get me to this page. Is it possible to make something like this work?

Moncada answered 18/2, 2014 at 16:51 Comment(1)
have you tried ng-src="{{view}}" ?Grappa
S
53

Try as follows:

<ng-include src="view"> </ng-include>

-----------------OR----------------------

You can do this way,

View:

<div data-ng-include data-ng-src="getPartial()"></div>

Controller:

function AppCtrl ($scope) {
  $scope.getPartial = function () {
      return 'partials/issues.html';
  }
}
Styptic answered 18/2, 2014 at 17:6 Comment(2)
<div data-ng-include data-ng-src="getPartial()"></div> does not work for me. getPartial() isn't rised, but <ng-include src="getPartial()"></ng-include> works!Williswillison
@Williswillison Try <div ng-include src="getPartial()"></div> instead of <div data-ng-include data-ng-src="getPartial()"></div>Hesler
C
11

Like this. You need to use single quotes and +, like you would in JavaScript.

<ng-include src="'/path/to/template'+ my.variable +'.html'"></ng-include>
Chelicera answered 15/2, 2017 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.