简体   繁体   中英

AngularJS child directives linking function firing multiple times for each parent directive on page

I'm trying to wrap my head around angularjs directives. I have a parent directive, with 4 instances of it on my page. One of the parent directives contains an instance of the CHILD directive.

The child directive's linking function appears to run 4 times however, as I see the following in my console 4 times:

child linking function

Any idea why / how to fix? Thanks!

PARENT:

Module.directive 'collapseWidget', () ->
  directive =
    restrict:   'A'
    transclude: true
    template:   viewCollapseWidget
    scope: 
      title:        '@title'
      widgetThemis: '@widgetThemis'
      color:        '@color'
      model:        '='

    #replace: true
    compile: (element, attrs, transclusionFunc) ->
      (scope, iterStartElement, attrs) ->

        #if scope.buttons
          #console.log scope.buttons
        scope.collapsed = false

        scope.toggle = () ->
          scope.collapsed = !scope.collapsed

        origElem   = transclusionFunc scope
        content    = origElem.text()
        scope.orig = content
        scope.obj  = content

CHILD:

Module.directive "myTable", ->
      directive =
        restrict: 'A'
        scope: 'isolate'
        link: (scope, element, attrs) ->
          console.log 'child linking function'
          return

Are you getting any console errors?

The reason I ask is that in your child 'myTable' directive you have scope defined as a String rather than an empty Object. It could be that scope is not isolated because of this and is using the parent scope instead.

I know I'm weighing in late, but the answer to this question is simple. The link function is going to get called as many times as you have instances of the directive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM