Angularjs 如何访问父指令变量?

Angularjs 如何访问父指令变量?,angularjs,Angularjs,如何访问父指令的变量?我一直没有定义 我正在尝试从productName访问productTitle。运行角度1.6 angular.module('appStore').controller( 'TestController', function($scope, Note){ $scope.yell = function(){ return Note(5); }; } ); angular.module('appSt

如何访问父指令的变量?我一直没有定义

我正在尝试从productName访问productTitle。运行角度1.6

  angular.module('appStore').controller(
    'TestController', function($scope, Note){    
      $scope.yell = function(){
        return Note(5);
      };
    }
  );

  angular.module('appStore').directive('productTitle', function(){
    return {
      restrict : 'E',
      templateUrl : 'product-title.html',
      scope : {
        age : '='
      },
      controller : function($scope){
          $scope.name = "mary";
      },
      link : function(scope, element, attrs){
      }
    };
  });

  angular.module('appStore').directive('productName', function(){
    return {
      restrict: 'E',
      templateUrl: 'product-name.html',
      require: '^productTitle',
      link : function(scope, element, attrs, ctrl){
    console.log('test');
        console.log(ctrl.name); // undefined???
        console.log(ctrl.age);  // undefined???
      },
      transclude: true

    };
  });
index.html:

<div ng-controller="TestController">
  <product-title age="6"></product-title>
</div>

product-title.html

<h3>Product Titles</h3>
<product-name></product-name>
产品名称

产品名称为空。

谢谢更新,我在这里猜了一下,但您能否在
productName
中设置
transclude:false
scope:false
来修复此问题?刚刚尝试过,什么也没有发生。。如果我使用console.log(ctrl),我只会得到构造函数()哦,哇。。。我必须将$scope.name=“mary”更改为this.name=“mary”才能使其正常工作。是的,我正要这么说。您可能已经看到了一个。