javascript - 在我的案例中如何检测点击事件

标签 javascript html angularjs modal-dialog

我试图在父 Controller 上弹出一个模式,并在使用单击按钮后对子 Controller 执行某些操作

例如, 子 Controller

$scope.openModal = function(){
    var item = 'my first item'
    $scope.showItem(item);

    //I want to do something here after user clicks 'Click me' button
})

父 Controller

$scope.showItem = function(item){
    $modal({
                template: 'item-modal.html',
                scope: $scope,                
          });
})

在我的 item-modal.html 中

//other html
<button type="button" class="btn btn-primary" ng-click="confirm()">Click me</button>

所以我希望 child 能够知道 parent 的操作,即单击的模式按钮。有没有办法做到这一点?非常感谢!

最佳答案

您可以通过$broadcast监听事件来监听父链下的事件。

// Parent controller
$scope.confirm = function() {
  $scope.broadcast("myTestEvent", {'key':'value'});
}


// Child controller
$scope.$on("myTestEvent", function(data) {
  console.log("Event from parent fired with data as : " + data );
})

关于javascript - 在我的案例中如何检测点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31993708/

相关文章:

javascript - 使用 .htpasswd 锁定 .user.js 文件

html - 设置同一行的两个div居中显示

javascript - Angular View 中缺少 CSS 类

php - 将表单发送到 SQLdatabase(不允许 405)

javascript - 从 javascript 中的自定义属性中选择一个值

javascript - 如何在 AngularJs Promise 中获取使用的 http 调用参数?

javascript - Angularjs 指令

php - 使用 html 代码附加字符串

javascript - JDK 1.8.0_92 Nashorn JS 引擎 indexOf 行为

javascript - 从返回 JSON 的 url 输出文本