1

While the code below "works", it definitely feels very wrong. Is there a "best practices" way to refactor? The issue is that I'm passing an instance of the controller into an isolate scope directive

(note: I can't use this in the directive:

controller: 'ViewController', controllerAs: 'viewCtrl',

because I need the same instance of the controller..)

It may just be the case that I didn't set this up correctly in the first place, but curious to know if there is a good way to "refactor" this

UI-Router config:

  .state('someState', {
    url: '/someurl',
    views: {
      '' : {
        templateUrl: '/path/to/template.html',
        controller: 'ViewController',
        controllerAs: 'viewCtrl'
      },

The Directive

        <some-dirctive
          view-ctrl="viewCtrl">
        </some-dirctive>
Emil L
  • 51
  • 1
  • 3
  • If you need the isolate scope, then you pass arguments to the directive, not a controller. You could achieve it by doing a require: '^ngController' and in the function link (scope, element, attrs, outerCtrl) (didn't test) – Alexander Feb 25 '16 at 21:33
  • You should probably be using some sort of service that both your controller and directive can access rather than trying to pass a controller around. – Scott Feb 25 '16 at 22:43
  • Alexander, Scott, thanks much for your comments. My apologies, I don't think I provided enough context. the viewCtrl (the instance of ViewController in the template/view) holds reference to an object (viewCtrl.myObject, which is set inside the controller) that I want to access in the directive. I think my overall design is just off, and I probably shouldn't be using a directive -- or I can use a $broadcast. My apologies that my question is not clear / poorly worded. – Emil L Feb 26 '16 at 23:07

0 Answers0