I have elements that I'd like to display on many pages, and I think I'm
doing it in a stylistically undesirable way. All my AngularJS experience
has been self-directed, so what I'm hoping is that someone will say "I'm
surprised that even works; here's a link to how everybody really does it."
Not that I haven't spent a lot of time looking for a link like that
already...

As an example of an element I'd like to display on many pages, I have a
"Name" element that looks something like this:

+----------------+
| Bilbo Baggins  |
| *Edit*           |
+----------------+

And a "Contact Info" element that looks similar:
+--------------------------------+
| Contact Name: Sam Gamgee       |
| Email Address: [email protected] |
| Phone number: (123) 456-7890   |
| Physical Address: <etc.>       |
| *Edit*                           |
+--------------------------------+

There are lots of pages I'd like to show this on, because I have a lot of
different sorts of things that have Names, and a lot of different sorts of
things that have Contact Infos. My solution right now is to do something
like this:

<ng-include src="'/fields/name.html'"
            ng-controller="DisplayNameController"
            onload="data=vendor_name_data">
</ng-include>

...where $scope.vendor_name_data is filled in by the active controller:
VendorController. *Is this a good approach?* The only advantage: It seems
to work. Disadvantages: I have to be careful never to overwrite
vendor_name_data; only to change its fields. I also have this "parameter"
to DisplayNameController, data, which I want to use in the controller and
just have to expect to be there.


Now here's a trickier one, which comes up when I hit the *Edit* button.
Whichever one I hit, it opens a modal
<https://angular-ui.github.io/bootstrap/#/modal> implemented in
editor_modal.html and editor_modal.js. Now comes the snippet of code I'd
like to use in editor_modal.html:
<ng-include ng-repeat="editor in editors"
            ng-controller="editor.controller"
            src="editor.templateUrl"
            onload="data=editor.data">
</ng-include>

The problem is that ng-controller wants the name of the controller, not a
variable that resolves to the name of the controller, and I don't know how
to get around that. So as a workaround, I put <div
ng-controller="EditNameCtrl"> at the top of the html template that's being
included; but then "onload" stops doing the trick. The result, which works
but I know is explicitly warned against in the documentation for ngInit:
<ng-include ng-repeat="editor in editors"
            src="editor.templateUrl"
            ng-init="data=editor.data">
</ng-include>

So how can I make an ng-include work if I only have the string for the
controller once I know what's being included?

Thanks for your time,
Bartholomew

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to