Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify model to a ngInclude directive in AngularJS?

I would like to use the same HTML template in 3 places, just each time with a different model. I know I can access the variables from the template, but there names will be different.

Is there a way to pass a model to the ngInclude?

This is what I would like to achieve, of course the attribute add-variable does not work now. Then in my included template, I would acces the detailsObject and its properties.

<pane title="{{projectSummary.ProjectResults.DisplayName}}">     <h2>{{projectSummary.ProjectResults.DisplayName}}</h2>     <ng-include src="'Partials/SummaryDetails.html'" init-variable="{'detailsObject': projectSummary.ProjectResults}"></ng-include> </pane>  <pane  title="Documents" header="true"></pane>  <pane ng-repeat="document in projectSummary.DocumentResults" title="{{document.DisplayName}}">     <h2>{{document.DisplayName}}</h2>     <ng-include src="'Partials/SummaryDetails.html'" add-variable="{'detailsObject': document}"></ng-include> </pane>  <pane ng-repeat="header in [1]" title="Languages" header="true"></pane>  <pane ng-repeat="language in projectSummary.ResultsByLanguagePairs" title="{{language.DisplayName}}">     <h2>{{document.DisplayName}}</h2>     <ng-include src="'Partials/SummaryDetails.html'" add-variable="{'detailsObject': language}"></ng-include> </pane> 

If I took a bad approach with using ng-include, is there something else I should try?

like image 489
Tomas Grosup Avatar asked Nov 16 '12 19:11

Tomas Grosup


People also ask

Is NG model an AngularJS directive?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value.

Which of the below is required with Nginclude directive?

The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename. By default, the included file must be located on the same domain as the document.

Which directive is used to assign values to the variables in AngularJS?

ng-init directive It is used to declare and assign values to the variables for an AngularJS application.

What is NG model options in AngularJS?

The ng-model-options directive is used to control the binding of an HTML form element and a variable in the scope. You can specify that the binding should wait for a specific event to occur, or wait a specific number of milliseconds, and more, see the legal values listed in the parameter values below.


1 Answers

There is a rather simple solution, although I must admit, it's not what Misko would recommend. But if creating a directive is an overkill for you and getting Brice's patch is not feasible then the following will help you.

<div ng-repeat="name in ['A']" ng-include="'partial.html'"></div> <div ng-repeat="name in ['B']" ng-include="'partial.html'"></div>  <script type="text/ng-template" id="partial.html">    <div>{{ name }}</div> </script> 

It's quite evident why it works. See an example here: http://jsfiddle.net/Cndc6/4/

like image 129
Denis Pshenov Avatar answered Sep 22 '22 02:09

Denis Pshenov