javascript - AngularJS directive: Reference local variables from external HTML file used in template -


i hope phrased question correctly, haven't been able find else on this. have angularjs directive, this:

angular.module( 'example', [] ).directive(         'exampledirective', ['$compile', '$http',         function($compile, $http) {             return {                 restrict: 'a',                 link: function (scope, element, attrs) {                      var listofitems = attrs.listofitems;                     var foo = attrs.foo;                     var template =                         '<ul>' +                             '<li ng-repeat="item in ' + listofitems + '">'+                                 '<i ng-click="clicked(item)">'+                                     '{{item.' + foo + '}}'+                                 '</i>'+                              '</li>'+                          '</ul>';                      element.html('').append($compile(template)(scope));                 }              };           }]); 

listofitems being array objects [{'name': 'this'}, {'name': 'that'}] , foo being name.

and want place template in it's own html file , load http call. how template in html file? since wont able access local variable in file i'm not sure needs changed. or comments appreciated, thanks.

your external template have access scope variables.

updated directive:

angular.module( 'example', [] ).directive(   'exampledirective', ['$compile', '$http',     function($compile, $http) {       return {         restrict: 'a',          // ...           templateurl: 'my_template.html',          // ...       };     }   ] ); 

my_template.html:

<ul>   <li ng-repeat="item in ' + listofitems + '">     <i ng-click="clicked(item)">       {{item.' + foo + '}}     </i>   </li> </ul> 

Comments

Popular posts from this blog

command line - Use qwinsta in PowerShell ISE -

java - Incorrect order of records in M-M relationship in hibernate -

Python website log in: BIG-IP can not find session information in the request -