Javascript Angularjs ng如果不工作,但日志布尔值显示预期值

Javascript Angularjs ng如果不工作,但日志布尔值显示预期值,javascript,angularjs,coffeescript,Javascript,Angularjs,Coffeescript,我有一个指令,它有一些基本逻辑,可以隐藏和显示页面上的文本项。这种逻辑在一半的时间里是正确的,但在另一半的时间里是不正确的。我在指令中的代码是: newco.directive 'heroHeadline', ($routeParams, $interpolate, $rootScope, paramsStateService) -> restrict: 'E' templateUrl: '/pages/shared/hero_headline' scope: marq

我有一个指令,它有一些基本逻辑,可以隐藏和显示页面上的文本项。这种逻辑在一半的时间里是正确的,但在另一半的时间里是不正确的。我在指令中的代码是:

newco.directive 'heroHeadline', ($routeParams, $interpolate, $rootScope, paramsStateService) ->
  restrict: 'E'
  templateUrl: '/pages/shared/hero_headline'
  scope:
    marquee: "="
  link: (scope, el, attrs) ->
    scope.displayInternationalCopy = false
    scope.displayHeadline2 = false
    if paramsStateService.customerCategoryId is 'international'
       scope.displayInternationalCopy = true
    else
      classification = $routeParams.classification
      classification ?= 'us'
      doubleHeadlineClassifications = ["latino", "us"]
      scope.displayHeadline2 = if classification in doubleHeadlineClassifications then true

    console.log(scope.displayInternationalCopy)
我的模板是:

p.uppercase(ng-if="!displayInternationalCopy")
  | Dishworld is now Sling International
  h1.invert
    span.copy The #1 International
    span.copy TV Service in the U.S.
  p Sign up today for as low as $25/mo. (and sometimes lower!)
h1#marquee-headline-1.invert ng-if="displayInternationalCopy"
  interpolate(value="{{ marquee.headline1 | heroHeadline | perMonth }}" params="basePack")
h2#marquee-headline-2 ng-hide="displayHeadline2 "
  interpolate(value="{{ marquee.headline2 | heroHeadline | perMonth }}" params="basePack")
  | {{displayHeadline2}}
displayInternationalCopy的console.log显示为false,但模板的顶部仍在呈现。对于另一种情况,当displayInternationalCopy计算为true时,正确显示。我已经在应用程序的几个地方使用了这种模式,没有任何问题,我只是在这一点上感到困惑

此外,显示指令的视图为:

section.jumbotron#international style="background-color: #000" ng-class="{lefty: isDomestic, white: !isDomestic }"
  img#hero.hidden-xs height='616'  src="{{ marquee.heroImage }}"

  .p ng-class="{hero_gradient: !isDomestic}"
  .transbox.visible-xs
  .copy-international
    .container-fluid
      .container
        hero-headline(marquee="marquee")
        a.btn.btn-primary.btn-lg#watch_now(role="button" rel="nofollow" ng-click="signUp($event); toggleHBO(false); " user-flow="")
          | {{ classification.cta_button }}
        hero-attribution

您在指令中使用的是隔离作用域,该指令创建的新作用域不是从父级继承的。也就是说,您不能直接在指令内部访问父作用域。您需要使用$parent。要引用父作用域,如作用域。$parent.displayInternationalCopy,或者您需要在独立作用域内包含displayInternationalCopy变量,如displayInternationalCopy:“=”,以便父级的displayInternationalCopy作用域变量将在您的指令作用域内可用。

它应该是作用域。$parent.displayInternationalCopy内部指令..因为指令使用的是隔离作用域..或使用attributeIt在隔离作用域中传递displayInternationalCopy肯定是一个范围问题。我希望我能把你作为答案。谢谢。我已经添加了一个答案