0

I can't seem to get the following code to work:

<script>alert(topic);</script> <!-- Outputs: "dynamics" -->

<div ng-include="'content/' + topic + '.html'"></div> <!-- Does not work. -->

I have deduced the variable is the problem as the following code does work:

<div ng-include="'content/' + 'dynamics' + '.html'"></div> <!-- Works. -->

Does anybody know how I can do this?

Update:

Following Steffen's link, I have written the following code, but still no luck:

<script>
    alert(topic); // Outputs "dynamics"

    var app = angular.module('myapp', []);

    app.controller('MainCtrl', ['$scope', '$window', function ($scope, $window) {
        $scope.topic = $window.topic;
    }]);
</script>

<div ng-app="myapp" ng-controller="MainCtrl" ng-include="'content/' + 
    topic + '.html'"></div>  <!-- Does not work. -->

Thanks.

  • http://stackoverflow.com/questions/19383725/how-to-access-global-js-variable-in-angularjs-directive – Steffen May 15 '16 at 12:22
  • Please remove the curly braces from ng-include attribute – Steffen May 15 '16 at 13:04
  • use this in html : `
    ` OR `
    `
    – bob May 15 '16 at 13:04
  • I'm sorry, but neither is working for me. – user6336941 May 15 '16 at 13:11
  • Try this: https://jsfiddle.net/ursujuge/1/ You can see in the network console of your browser that it is trying to load dynamics.html – Steffen May 15 '16 at 13:29
  • To give a bit of an explanation. The value of most angular directives (like ng-include) is evaluated as javascript in context of $scope. That is why your variable has to be declared in the current $scope. Double curly braces will lead to an error because it is not valid js syntax. Other directives like ng-href allow a string based syntax like `` which often leads to confusion. – Steffen May 15 '16 at 13:41
  • Thank you so much @Steffen , after a good nights sleep I have got it to work. This has really opened my eyes to how AngularJS works as well. Thanks again. – user6336941 May 15 '16 at 23:35

3 Answers3

1

Based on Steffen's jsfiddle, here is how I passed a JavaScript variable to AngularJS and used it in defining a directory:

<script>
    // Create module.
    var app = angular.module('app', []);

    // Add controller to module.
    app.controller('MainCtrl', ['$scope', '$window', function ($scope, $window) {
        $scope.topic = $window.topic;
        console.log($scope.topic);
    }]);
</script>

<div ng-app="app" ng-controller="MainCtrl" ng-include="'content/' + 
    topic + '.html'"></div> <!-- Works! -->

Many thanks to all for their answers. :)

0

try this :

<div ng-include="'content/{{topic}}.html'"></div>

In angularjs , scope variable are access in HTML using The double curly brace notation {{ }} .

bob
  • 4,595
  • 2
  • 25
  • 35
0

Try as follows:

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

Make sure you have define $scope.topic = "whateveryouwant";

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

You could do it like:

<ng-include src="getTopic()"></ng-include>

Controller:

function AppCtrl ($scope) {
  $scope.getTopic= function () {
      return 'partials/whatever.html';
  }
}
Wcan
  • 840
  • 1
  • 10
  • 31