angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ng-include document fragments

Open ejain opened this issue 14 years ago • 8 comments

If I do this:

  <div ng-include="dialogs.html#sign-in"></div>

angular retrieves the dialog.html file, but includes the entire file, not just the #sign-in div. Same for templateUrl in directives.

This feature would be useful for applications that have a lot of small includes (e.g. for dialogs), but don't want to inline them on the main page (which slows down loading of the first page) or have them retrieved one by one.

ejain avatar May 09 '12 05:05 ejain

+1 for this one

hkdobrev avatar Jul 06 '12 07:07 hkdobrev

+1

The ng-include should provide same flexibility as the one of JQuery load() function:

http://api.jquery.com/load/

Ex:

jQuery('#myPanel').empty();

jQuery('#myPanel').load('fragments/myFragment.html #interstingDiv', function() { ... ... });

doanduyhai avatar Oct 11 '12 20:10 doanduyhai

+1

I like this idea.

jlmakes avatar Oct 14 '12 10:10 jlmakes

+1

It would be usefull.

Yukilas avatar Oct 19 '12 16:10 Yukilas

+1

If you are using ng-grid and cell rendering templates it would be nice to have them all in one file rather than a bunch of little files (I prefer to manage them in html vs js)

jadrake75 avatar Jun 18 '13 04:06 jadrake75

+1 that it would be ideal for ngInclude to be a drop-in alternative to jQuery.load()

I have two motivations for this:

(1) My motivation is that I have projects which I am retrofitting with Angular, and these projects will sometimes use jQuery.load() with selectors.

(2) I have the same motivation as the OP. I am aware that pre-warming the templateCache is a workaround for this, but it would still be nice to have the option to load fragments anyways.

I've proposed a patch that uses Angular's .find() function and is implemented identically to jQuery.load(). It retains full backwards compatibility with ngInclude. The only caveat is this: the types of selectors that can be used are limited to Angular's .find(). This means that if using Angular alone, it will be limited to selecting only element names (e.g. div or span) - to use full selectors, you'll need to include jQuery, and then #foo or .stuff > div > div >div can be used, i.e. for example:

<div ng-include="'dialogs.html div'"></div> <!-- pure Angular (no jQuery) -->
<div ng-include="'dialogs.html #sign-in'"></div> <!-- jQuery+Angular -->
<div ng-include="'dialogs.html #sign-in .stuff > div'"></div> <!-- jQuery+Angular -->

dhbaird avatar Jul 06 '13 21:07 dhbaird

The AngularUI project now offers uiInclude. uiInclude is identical to the Angular 1.0.x ngInclude (hence, no animation) with a new extra fragment attribute. It works like this:

<ui-include src="'url/to/partial/file'" fragment="'#id-to-fragment'"></ui-include>

When writing your Angular app, the following extra two lines will enable you to use uiInclude:

<script src="include.js"></script>
app = angular.module('myApp', ['ui.include']);

Note that jQuery is required for full selector support, otherwise it falls back to jqLite which supports very limited selectors. Also note that both src and fragment are Angular expressions, and therefore string literals must be quoted. I'd be all in favor of moving this capability over to Angular if the Angular devs think it is appropriate.

dhbaird avatar Jul 30 '13 20:07 dhbaird

Based on discussion in https://github.com/angular/angular.js/pull/3151 I think that we can close this one.

pkozlowski-opensource avatar Jul 30 '13 21:07 pkozlowski-opensource