【问题标题】:ng-show not working with ng-includeng-show 不能与 ng-include 一起使用
【发布时间】:2016-05-04 01:10:57
【问题描述】:

http://plnkr.co/edit/0CAgXsX847n9LfIG4Fzj?p=preview

我有两个文件: -index.html(这使用 ng-include - 不工作) -index_embedded.html(使用相同的代码但不使用包含 - WORKS)

您好,我的问题是当我点击“CLICK ME”,然后点击“CLOSE”,然后再次尝试点击“CLICK ME”时,它不再起作用。

当我使用相同的代码而不使用 ng-include 时,我可以点击“CLICK ME”、“CLOSE”,然后再次点击“CLICK ME”。

为什么只有当代码在包含文件中时这不起作用?

<h2 ng-click="asdf = !asdf">CLICK ME</h2>

<!-- <div ng-include="'partials/audiencenav.html'"></div> -->
<!-- extracted code from the include and pasted below, i'm able to successfully  CLICK, CLOSE, and CLICK again -->
<div class="" ng-show="asdf">
    <div ng-click="asdf = !asdf"><h2>CLOSE</h2></div>   
</div>  

问题是当代码被放置在包含文件中时,它会在点击“CLOSE”后中断。

【问题讨论】:

    标签: angularjs partials ng-show angularjs-ng-include


    【解决方案1】:

    第一次它从父级继承 asdf 的值,直到您单击 CLOSE 后没有单击关闭,它正在创建 asdf 具有本地范围,不再受单击 CLICK ME 的影响,因此您可以在始终引用父级的包含文件中使用 $parent.asdf。

    请查看更新的插件

    <div class="" ng-show="$parent.asdf">
        <div ng-click="$parent.asdf = !$parent.asdf"><h2>CLOSE</h2></div>   
     </div>
    

    http://plnkr.co/edit/9wmKTR8HPw54K95XqapA?p=preview

    【讨论】:

    • 对我帮助很大,上帝保佑你的编码技巧
    【解决方案2】:

    我认为问题在于 ng-include 创建了新范围,而 asdf 被该范围的本地版本所遮蔽。

    这是众所周知的问题。您可以将变量放在内部结构中(例如将 asdf 更改为 input.asdf 并将输入初始化为 {})或(更高级)使用控制器作为构造。

    看这里:https://stackoverflow.com/a/14146317/115198

    【讨论】:

      【解决方案3】:

      这是因为使用 ng-include 在包含文件中创建了新的子范围,而外部范围成为父范围。

      所以你应该在 _include.html 中尝试如下方式

      <div class="" ng-show="$parent.asdf">
          <div ng-click="$parent.asdf = !$parent.asdf"><h2>CLOSE</h2></div>   
      </div>
      

      希望对您有所帮助!

      【讨论】:

      • 我的答案的完整副本。
      • 我还没有看到你的回答人...我只是在发布后才看到它!
      猜你喜欢
      • 1970-01-01
      • 2016-10-29
      • 2016-04-24
      • 2018-02-27
      • 2018-03-27
      • 2016-10-06
      • 2014-01-22
      • 2015-06-29
      • 1970-01-01
      相关资源
      最近更新 更多