从Angular 1.5中的父组件调用子组件函数

Str*_*der 5 angularjs

在我的父组件的模板中,有一个嵌套组件.

<div class="inner" ng-show="!vm.loading">
    <div class="info-panel">
        <h3 id="basePrice">Current Cost</h3>
        <p>
            <small>
                <a href role="button" data-toggle="modal" ng-click="vm.openCostsModal()">See Details</a>
            </small>
        </p>
    </div>
    ......
    ......
    <pricingcalculation></pricingcalculation>
Run Code Online (Sandbox Code Playgroud)

<pricingcalculation></pricingcalculation>是嵌套组件.它的定义如下:

(function () {
    "use strict";
    angular.module("app")
        .component("pricingcalculation", {
            transclude: true,
            templateUrl: "/static/angtemplates/pricing-calculation.html",
            controllerAs: "vm",
            controller: ["$rootRouter", function ($rootRouter) {
                var vm = this;
                vm.openCostsModal = function () {
                    vm.costItems = [];
                    vm.projectedCostItems = [];
                    vm.totalOfCostItems = 0;
                    .....
                    .....
Run Code Online (Sandbox Code Playgroud)

因此,在父See Details按钮模板上定义的按钮单击

我希望调用子组件的函数openCostsModal().

怎么做 ?

Fre*_*ego 11

您可以$broadcast在父母中使用广播事件,并$on在您的孩子中使用它来收听.

像这样:

在父母:

$scope.$broadcast("someEvent"); 
Run Code Online (Sandbox Code Playgroud)

在孩子:

$scope.$on("someEvent",function(){
  //do stuff here
});
Run Code Online (Sandbox Code Playgroud)