Communicating with sibling directives

后端 未结 6 727
余生分开走
余生分开走 2021-01-31 10:19

Goal: Create behaviors using directives with communication between 2 sibling elements (each their own directive).

A behavior to use in example: The article content is hi

6条回答
  •  甜味超标
    2021-01-31 10:52

    If there is a list of articles and its content we can do it without any directive, using ng-repeat

    {{article.header}}
    {{article.content}}

    So you need to define the article model in controller. We are making use of local scope created by ng-repeat.

    Update: Based on your feedback, you need to link them together.You can try

    this is my header
    this is content for the header above

    and in your directive

    use

    link: function(scope, element,attrs) {
          element.bind('click', function(){
            $('#'+attrs.content).show();
          }
        }
    

    And the final method could be to use $rootScope.$broadcast and scope.$on methods to communicate between to controllers. But in this approach you need to track from where the message came and who is the intended recipient who needs to process it.

提交回复
热议问题