0

I'm using the controller as syntax, and not sure how to access the child directive's scope. I can do it like this, but there has to be a better way, no?

angular.module('saProducts').directive 'saProductNew', ->
  restrict: 'A'
  templateUrl: 'app/products/templates/new.html'
  controllerAs: 'product'
  controller: ($scope, Product) ->

    @save = ->
      Product.save(product: @current).$promise.then (product) ->
        $scope.$$childHead.images.owner = product # Isn't there a way to access it without going through the $$childHead object?
Nathan
  • 7,627
  • 11
  • 46
  • 80

1 Answers1

0

You have additional three solutions that I can think of:

  1. Use a service to share data between parent and child scopes.

  2. Use emit/broadcast to broadcast changes from child to parent.

  3. Create the child property in the parent scope. So for instance, in parent directive, $scope.p = {c: "change from"}, then you can access that in child scope as, $scope.p.c = "changed to", now if you access it in the parent, it will be displayed as "changed to" due to prototypal inheritance.

I would personally use 1st option if its a complex application, followed by nr 3 for simple stuff, and lastly option nr 2.

Samir Alajmovic
  • 3,247
  • 3
  • 26
  • 28
  • This answer is correct. I would go with 1 or 2. Here is a good link relating to services http://viralpatel.net/blogs/angularjs-service-factory-tutorial/ and here is one for events - http://stackoverflow.com/a/14502755/852806. I use both depending on circumstances. – JsAndDotNet Mar 04 '15 at 11:42
  • Ok, thanks. I tried a broadcast, but it doesn't give me the response value of the method called. I need it's promise. Anyway to do that? – Nathan Mar 04 '15 at 12:21