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

How to detect a click event in my case

我正在尝试在 parent 控制器上弹出一个模式,并在使用后对 child 执行一些操作

例如, child 控制器

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

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

Parent 控制器

$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 );
})

angular-ui模态对吧?

如果是这样,则 $modal api returns 承诺在使用 $close(成功)或 $dismiss(失败)关闭模态时解决。有关承诺的信息,请参阅 https://angular-ui.github.io/bootstrap/#/modal for more info, or https://docs.angularjs.org/api/ng/service/$q。

所以这看起来很像这样:

$scope.openModal = function(){
    var item = 'my first item'
    $scope.showItem(item).then(function(result) {
      // DO something with result
    });
});

父控制器

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

在我的项目中-modal.html

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

或者使用您自己的方法作为 ng-click 处理程序,它将调用 $scope.$close 以及您想要作为承诺结果传回的任何内容。