0

I'm using scopeless directives (https://github.com/johnpapa/angularjs-styleguide#directives-and-controlleras).

<directive1>
    <directive2></directive2>
</directive2>

In outer directive1 I've got controllerAS: 'vm' with var vm=this; vm.test="test from directive1" in it.

Inner directive2 has scope: false, controllerAs: 'vm2', bindToController:true How from directive2 get directive1 vm.test data? I want to use it in controller - not HTML view

SoEzPz
  • 14,958
  • 8
  • 61
  • 64
piernik
  • 3,507
  • 3
  • 42
  • 84
  • It does not mean it is scopeless, it just uses parent scope. You can get it via `scope.vm` itself – PSL Feb 06 '15 at 15:14
  • I mean that I don't want to use `$scope`. Read this https://github.com/johnpapa/angularjs-styleguide#controlleras-view-syntax – piernik Feb 06 '15 at 16:57
  • Problem is with your design... Why does your directive has a hidden dependency on parent controller, if so then just use require in your directive. See this [answer](http://stackoverflow.com/questions/14914213/when-writing-a-directive-how-do-i-decide-if-a-need-no-new-scope-a-new-child-sc/14914798#14914798) on how to scope your directive. trying to access it with your specific case is an X/Y problem due to design issue, i believe. – PSL Feb 06 '15 at 17:00

1 Answers1

0

What you are trying to do with

var vm = this

is a local variable to directive 1's JS scope (not angular's $scope object). Directive2 will never have access to it. To provide access you will have to attache vm to directive one's scope. Then it will be accessible to the nested directive two's $scope.

Like this in directive one

$scope.vm = this; // This will work
$scope.vm.test = 'test from directive1 scope';

Like this in directive two

controller: function ($scope) {
  console.log($scope.vm.test) // this will work for you

Here is a working jsFiddle example.

working jsFiddle example

SoEzPz
  • 14,958
  • 8
  • 61
  • 64
  • OP is using controllerAs syntax, so vm/vm2 aliases are not some local variable it is a property on the scope already. – PSL Feb 06 '15 at 16:12
  • But I don't want to use `$scope` at all: read this https://github.com/johnpapa/angularjs-styleguide#controlleras-view-syntax – piernik Feb 06 '15 at 16:57
  • Perhaps you can use the jsFiddle I provided and test your theory? I simply can't call $scope.vm or scope.vm in directive2 and get the value. What do you propose in a working jsFiddle example? – SoEzPz Feb 06 '15 at 16:59
  • Ah, I see your example, thank you for posting it for clarification. – SoEzPz Feb 06 '15 at 17:02