1

Based on these set-ups (Angular UI-Router testing scope inheritance, Angular ui-router - how to access parameters in nested, named view, passed from the parent template?), I did the following (the third holds the relevant issue):

  .state("patients", {
    url: "/dashboard/patients",
    templateUrl: 'patients/index.html',
    controller: "patientCtrl"
  })
  .state("sharedPatients", {
    url: "/dashboard/patients/shared",
    templateUrl: 'patients/shared_patients.html',
    controller: "patientCtrl"
  })
  .state('showPatient', {
    url: "/dashboard/patients/:id",
    templateUrl: 'patients/show.html',
    controller: ("patientCtrl", ['$scope', '$stateParams', function($scope, $stateParams) {
      $scope.patient_id = $stateParams.id;
    }])
  })

Patients and sharedPatients work without a problem. I can also go to showPatient and access the variable patient_id. However, I cannot access any of the functions or variables established in patientCtrl. Thoughts?

Community
  • 1
  • 1
Morgan
  • 1,438
  • 3
  • 17
  • 32

1 Answers1

1

The controller scope inheritance has nothing to do with the state inheritance.

Your controllers only inherit from each other if their views are nested in the DOM.


Also, the syntax you're using there is misleading. controller: ("patientCtrl", [...]) will just ignore that first part. It'll only use the controller inside the array.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Still unclear. Here's my thinking: Each state is associated with a controller. I have the showPatient state and, like the states before it, I want it associated with the controller patientCtrl. I didn't expect it to automatically inherit that ctrl; I set it explicitly. However, I also need to have access to the :id parameter of its url, which I understood could be done using stateParams like above. However, this is not working within my template; I only get access to the patient_id. – Morgan Apr 11 '14 at 18:51
  • Just saw your update: yes, that's exactly the point I'm confused about. How do I get access to both a Ctrl and stateParam values within a template? – Morgan Apr 11 '14 at 18:54
  • Okay; after researching the controller inheritance (which is not what I was trying to do, but I'd misunderstood the examples I'd seen), I see why this doesn't work. Still working on getting something functional, however. Half way there; thanks, Joseph. – Morgan Apr 11 '14 at 19:09