Automatically instantiate AngularJS controller nested in templateUrl

tags2k

I'm just learning Angular and have a very basic app set up. When rendering some data via the templateUrl property of a route, what would be the best way to include a sub-controller in the returned template? For example, including a "createOrEditItem" template at the bottom of a "viewItem" template so that the "createOrEditItem" can be reused on its own later?

I've tried putting a div in the template with its ng-controller attribute set to a controller name that I've defined at the app level, but it's not being activated. Should this be done with a directive instead to make it instantiate when the master controller has its contents set, or am I missing something more fundamental?

tanmay

yes, as mentioned in the later part of the question, you should be using a directive. Or, if using AngularJS >= v1.5, component should be the choice because they are pluggable and works well with nesting too.

Note that for the route also, you can directly use a component like this:

var myMod = angular.module('myMod', ['ngRoute']);
myMod.component('home', {
  template: '<h1>Home</h1><p>Hello, {{ $ctrl.user.name }} !</p>',
  //                    ^^^^ other components can be used here
  controller: function() {
    this.user = {name: 'world'};
  } 
});

myMod.config(function($routeProvider) {
  $routeProvider.when('/', {
    template: '<home></home>'
  });
});

Now, as the comment suggests, you can freely use other components in the template of home component.

Hope this helps a bit!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Instantiate and Inject into AngularJS Controller

From Dev

Access element inside templateUrl from controller - AngularJS

From Dev

AngularJS Controller not working in nested Controller

From Dev

Controller in Nested views of AngularJS

From Dev

Controller in Nested views of AngularJS

From Dev

AngularJs multiple instances and nested controller

From Dev

AngularJS automatically updating controller data by service

From Dev

setInterval function in AngularJS controller is being called automatically

From Dev

Why does nested directives Controller and Link functions trigger order depend on template and templateUrl?

From Dev

angularjs using variables in templateUrl

From Dev

angularjs component with dynamic templateUrl

From Dev

templateUrl vs template in AngularJS

From Dev

Using directives in angularjs with templateUrl

From Dev

Angularjs routeProvider templateURL not working

From Dev

AngularJs override templateUrl function

From Dev

angularjs 1.5 override templateUrl

From Dev

AngularJS: Initialize nested scope variables in controller

From Dev

How do I initialize a nested controller in AngularJS?

From Dev

Use ng-model in nested Angularjs controller

From Dev

angularjs nested controller does not receive injected service

From Dev

Use ng-model in nested Angularjs controller

From Dev

Automatically update selected option in the angular controller to a select within a nested repeat

From Dev

MVC: Instantiate Controller In Router?

From Dev

Unable to instantiate Angular controller

From Dev

Instantiate View Controller not working

From Dev

Instantiate View Controller not working

From Dev

Unable to instantiate Angular controller

From Dev

AngularJS Controller to update $scope automatically when backend data changes

From Dev

angularJs custom directive not working with templateUrl

Related Related

  1. 1

    Instantiate and Inject into AngularJS Controller

  2. 2

    Access element inside templateUrl from controller - AngularJS

  3. 3

    AngularJS Controller not working in nested Controller

  4. 4

    Controller in Nested views of AngularJS

  5. 5

    Controller in Nested views of AngularJS

  6. 6

    AngularJs multiple instances and nested controller

  7. 7

    AngularJS automatically updating controller data by service

  8. 8

    setInterval function in AngularJS controller is being called automatically

  9. 9

    Why does nested directives Controller and Link functions trigger order depend on template and templateUrl?

  10. 10

    angularjs using variables in templateUrl

  11. 11

    angularjs component with dynamic templateUrl

  12. 12

    templateUrl vs template in AngularJS

  13. 13

    Using directives in angularjs with templateUrl

  14. 14

    Angularjs routeProvider templateURL not working

  15. 15

    AngularJs override templateUrl function

  16. 16

    angularjs 1.5 override templateUrl

  17. 17

    AngularJS: Initialize nested scope variables in controller

  18. 18

    How do I initialize a nested controller in AngularJS?

  19. 19

    Use ng-model in nested Angularjs controller

  20. 20

    angularjs nested controller does not receive injected service

  21. 21

    Use ng-model in nested Angularjs controller

  22. 22

    Automatically update selected option in the angular controller to a select within a nested repeat

  23. 23

    MVC: Instantiate Controller In Router?

  24. 24

    Unable to instantiate Angular controller

  25. 25

    Instantiate View Controller not working

  26. 26

    Instantiate View Controller not working

  27. 27

    Unable to instantiate Angular controller

  28. 28

    AngularJS Controller to update $scope automatically when backend data changes

  29. 29

    angularJs custom directive not working with templateUrl

HotTag

Archive