1

I've created a new angular controller

angular.module('crmMailing').controller('crmMailingRecurScheduleCtrl', function($scope, $filter) {

I'd like to have access to the selectedMail object that other controllers (the ones built with .factory('crmMailingSimpleDirective') so I can use the ID in an API call but I don't have the chops to work out how to go about doing getting access.

Adding selectedMail to the call back params didn't work.

Defining the callback as EditMailingCtrl didn't work either.

Help a noob out?!

Here is the code in case you are interested in more context.

1

selectedMail is a function on the route´s resolve property, I believe you cannot inject it into another controller because the controller is instantiated in the route, but you can access the resolve property anywhere injecting the $route service like this:

...
angular.module('crmMailing').controller('crmMailingRecurScheduleCtrl', function($scope, $filter, $route) {
  var mailing_id = $route.current.locals.selectedMail.id;
...

Alternatively the way your directives are defined, the scope value is default to true, therefore you should be able to access the parent scope like var mailing_id = $scope.mailing.id;, mailing is the object holding the mailing settings.

6
  • $route.current.locals.selectedMail.id; worked a treat. I could not get a call to console.log(mailing) to work from inside the new controller that I had defined. Where should I expect mailing to be defined? – michaelmcandrew Apr 18 '17 at 17:36
  • Also, I realised that mailing was available in the template of the crmMailingRecurSchedule directive that I created and that it can be passed through to the $scope method as a parameter. I'm not sure if this is what you meant, or if I am doing it backwards, but it did work :) – michaelmcandrew Apr 18 '17 at 17:43
  • My fault, I meant $scope.mailing.id, I'll edit that – Andrei Apr 18 '17 at 17:48
  • oh wow, sneaky. a console.log($scope);console.log($scope.mailing);console.log($scope); makes me realise that angular is somehow intercepting calls to "undefined" properties and pulling them from somewhere. I wonder where/how... – michaelmcandrew Apr 18 '17 at 17:58
  • Some properties might be initially undefined, there might be a promise setting it, also console.log() is somehow out of angular's territory, that might be another reason? You can try using $log.log(), which is angular's service/ native way to log stuff, just remember to inject it into your controller before using it – Andrei Apr 18 '17 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.