Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaner way for defining Angular templates

Consider this code

my_app.run( [ '$templateCache' , function( $templateCache ) {
    var template = "" +
        "{{ item.name }}" +
        "<ul ng-if='item.sub'>" +
            "<li ng-repeat='item in item.sub' ng-include=\"'angular-multi-select-item.htm'\"></li>" +
        "</ul>" +
        "";
    $templateCache.put( 'angular-multi-select-item.htm' , template );
}]);

While this works as expected, I see it quite ugly (the concatenated string with html inside). Is there some other, more clean, way to define the html for an Angular template?

like image 744
alexandernst Avatar asked Jan 30 '26 16:01

alexandernst


1 Answers

What you think about this? Just create a view html view file in your working directory and load it with $http.

$templateCache.put('angular-multi-select-item.htm', $http.get('views/template.html'));

Your can also try it with `ng-include? (documentation) and add includes in the main view like:

<div ng-include="'views/template.html'"></div>
like image 190
lin Avatar answered Feb 01 '26 07:02

lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!