title: AngularJS - How to run controllers from other controllers?
body: <p>I'm new to angular, coming from a jquery way of thinking.  I've read through most of the main site and bits of others, and I'm trying to further learn by building a customizable dashboard application.</p>
  
  <p>My HTML:</p>
  
  <pre><code>    &lt;div class="library"&gt;
          &lt;div class="dropdown" ng-controller="LibraryCtrl"&gt;
              &lt;div id="lib-{{widget.WidgetID}}" ng-repeat="widget in widgets"&gt;
                  &lt;div data-toggle="tooltip" title="{{widget.WidgetDesc}}"&gt;
                      &lt;h2&gt;
                          {{widget.WidgetName}}&lt;/h2&gt;
                      &lt;div class="libraryWidget {{widget.WidgetReportType}}"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="dashboard" ng-controller="DashboardCtrl"&gt;
          &lt;div&gt;
              &lt;article&gt;
                  &lt;div id="dash-{{widget.WidgetID}}" class="jarviswidget" ng-repeat="widget in widgets"&gt;
                      &lt;header role="heading"&gt;
                          &lt;h2&gt;
                              {{widget.WidgetName}}&lt;/h2&gt;
                      &lt;/header&gt;
                      &lt;div role="content" class="content"&gt;
                          &lt;div class="inner-spacer"&gt;
                              &lt;div class="table"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/article&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Essentially, I am building one view for the "library" of available widgets, and another view for the widgets currently picked for their "dashboard".  The HTML is still pretty simple here.  The library view renders just fine, but the dashboard view does not.</p>
  
  <p>The JavaScript:</p>
  
  <pre><code>function LibraryCtrl($scope, $http, sharedModels) {
      $http({
          method: 'POST',
          url: '../Services/GetUserWidgetDetails',
          headers: {
              'Content-type': 'application/json'
          }
      }).success(function (data) {
          sharedModels.setProperty(data);
          $scope.widgets = data;
          console.log(data); // logs 2nd with expected data
      });
  }
  
  function DashboardCtrl($scope, sharedModels) {
      var libraryWidgets = sharedModels.getProperty();
      $scope.widgets = libraryWidgets;
      console.log(libraryWidgets); // logs 1st with empty object
  }
  
  angular.module('dashboard', [])
      .service('sharedModels', function () {
          var libraryModel = {};
  
          return {
              getProperty: function () {
                  return libraryModel;
              },
              setProperty: function (data) {
                  libraryModel = data;
              }
          };
      });
  </code></pre>
  
  <p>Both of my controllers ought to use the same model, so at the suggestion of other SO posts I've created the service to get and set this shared model.  The problem is, my DashboardCtrl tries to render its view before the success of LibraryCtrl.  <strong>What would be the angular way of solving this problem: making DashboardCtrl wait for LibraryCtrl's success?</strong>  </p>
  
  <p>Further, (and this may be deserving of being its own question/post,) if the div.content element in my div.dashboard view is going to one of many possible sub-templates depending on LibraryCtrl's success JSON, how might I go about doing this?  In jquery templating, I would have id's for each template, and jquery would render the HTML and append it where needed.  In angular, I'm not sure exactly how to pick a template and render inside of another template.  This need not be spelled out for me quite like my first bolded question above, but a point in the right direction would be great!</p>