【问题标题】:Firing a method of the child controller in the parent controller method在父控制器方法中触发子控制器的方法
【发布时间】:2014-10-09 06:50:53
【问题描述】:

我在两个单独的模块中有两个控制器(以及两个单独的 .js 文件),html 是这样的:

<div ng-controller="ParentCtrl as parent">
    .
    .
    .
    <div ng-controller="ChildCtrl as child">
        <table>
            .
            .
            .
        </table>    
    </div>

    <button ng-click="parent.cancel()">Cancel</button>

</div>

我的目标是在单击取消按钮时在 ChildCtrl 范围内清空表格。我在 ChildCtrl 中有用于清空表的 deleteAll() 方法,但是,我希望从 ParentCtrl.cancel() 内部调用该方法。根据我所做的一些研究,虽然我没有可行的解决方案,但我有以下选择:

(1) 使用服务(尽管就我所看到的示例而言,服务只共享数据,而不是方法)

(2) 使用 $rootscope

(3) 使用 $broadcast(再次使用 $rootscope)

什么是最好的方法以及如何做到这一点?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    你有没有参考下面这个链接,它有工作演示,它使用 $broadcast

    angularJS: How to call child scope function in parent scope

    希望对你有帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2014-01-11
      • 1970-01-01
      相关资源
      最近更新 更多