AngularJS: Cannot interpolate attribute from first directive to a second. (w/ plunker example)

故事扮演 提交于 2019-12-06 04:25:42

Well if you just want to pass the data from first directive to second directive template, then you can add the dynamics attribute in first directive controller using
this.fromFirstDir = "you can pass from here"

first directive controller :

controller: ['$scope','$attrs',function($scope,$attrs){
      console.log('controller');
      $scope.one = 'foo';

      this.fromFirstDir = "you can pass from here"
    }],
  }

Then using the require attribute in the secondDirective for first directive controller,you can access this dynamic attribute from link function of the secondDirective directive using controller passed to link function. Finally assign those attributes to local scope passed to link function.

app.directive('secondDirective',function(){
  return { 
    scope: {twoData : '@twoData'},
    require : '^firstDirective',
    template: function (element,attributes){
      console.log(attributes.two) //{{one}} not 'foo' or 'test'
      return 'Hello <b>{{fromFirstDir}}</b>'
    },
    link : function(scope,element,attr,firstDirCtrl){
      console.log("===",firstDirCtrl.fromFirstDir) 
      scope.fromFirstDir = firstDirCtrl.fromFirstDir;
    }
  }
});

In this way, those dynamic atributes are available to your second directive.

Here is the final fiddle.

Hope this will help you.

Did you write the second directive?

<second-directive two="'+ explicit +'"></second-directive>

For the above code to work you need to have an isolate scope object setup in the second directive, check out the plunkr below.

http://plnkr.co/edit/YP2h3MOhsrjN5sLUNQs6?p=preview

I am using your question to learn, but I was able find this, which might work for you:

app.directive("tryThis", function($compile){
    return{
        scope: {
          one: '@',
        },
        link: function(scope, element){
            var template = '<second-directive two="'+scope.one+'"></second-directive>';
            var linkFn = $compile(template);
            var content = linkFn(scope);
            element.append(content);
        }
    }
});

Plunkr is here, note that test is logged in the console now instead of {{one}}. If secondDirective is given an isolated scope, test will then show on screen.

This link also helped me to conceptually understand the problem you are facing, giving some context to the issue of 'no scope during compile' step - I'm not sure there is a way around this.

You don't (can't) have access to scope inside the template (because there is no scope at that moment). The template is used to create one or more elements and then they are linked to a scope (after having their controllers instantiated - if any).

There are a lot of ways to pass values between directives and each one is best suited for particular purposes. The simplest (but not necessarily best, depending on your usecase details) is assigning a value on a scope of the wrapper directive and let the inner directive read it off the scope:

<!-- HTML -->
<one for-two="{{test}}"></one>

// JS
angular.
  module('myApp', []).
  directive('one', oneDirective).
  directive('two', twoDirective);

function oneDirective() {
  return {
    restrict: 'E',
    scope: true,
    link: function onePostLink(scope, elem, attrs) {
      scope.two = attrs.forTwo;
    },
    template: '<two></two>'
  };
}

function twoDirective() {
  return {
    restrict: 'E',
    template: 'Hello, {{two}} !'
  };
}

You have transclude: true but are not using it in the template. Can't you just use this markup and have the template for first directive use <ng-transclude>? You have scope: true so you could just manipulate the property from the parent/controller and the changes would propogate to both directives.

markup

<first-directive one="test">
    <second-directive two="test"></second-directive>        
</first-directive>

template for first-directive

template: `<div>
               my first directive content
               <ng-transclude></ng-transclude>
           </div>`;

https://docs.angularjs.org/api/ng/directive/ngTransclude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!