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

Javascript 如何在我的案例中检测单击事件,javascript,html,angularjs,modal-dialog,Javascript,Html,Angularjs,Modal Dialog,我试图在父控制器上弹出一个模式,并在使用后单击按钮对子控制器执行某些操作 比如说,, 子控制器 $scope.openModal = function(){ var item = 'my first item' $scope.showItem(item); //I want to do something here after user clicks 'Click me' button }) $scope.showItem = function(item){

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

比如说,, 子控制器

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

    //I want to do something here after user clicks 'Click me' button
})
$scope.showItem = function(item){
    $modal({
                template: 'item-modal.html',
                scope: $scope,                
          });
})
$scope.showItem = function(item){
    return $modal({
                template: 'item-modal.html',
                scope: $scope,                
          });
})
父控制器

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

    //I want to do something here after user clicks 'Click me' button
})
$scope.showItem = function(item){
    $modal({
                template: 'item-modal.html',
                scope: $scope,                
          });
})
$scope.showItem = function(item){
    return $modal({
                template: 'item-modal.html',
                scope: $scope,                
          });
})
在my item-modal.html中

//other html
<button type="button" class="btn btn-primary" ng-click="confirm()">Click me</button>
//other html
<button type="button" class="btn btn-primary" ng-click="$close(myResult)">Click me</button>
//其他html
点击我

因此,我希望孩子能够知道家长的行为,即点击的模式按钮。有办法做到这一点吗?非常感谢

您可以通过
$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 );
})

角度用户界面模式,对吗

如果是这样,$modal api将在modal关闭时返回一个承诺,并使用$close(成功)或$DISCLESH(失败)。有关更多信息,请参阅;有关承诺的信息,请参阅$q

所以这看起来就像这样:

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

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

    //I want to do something here after user clicks 'Click me' button
})
$scope.showItem = function(item){
    $modal({
                template: 'item-modal.html',
                scope: $scope,                
          });
})
$scope.showItem = function(item){
    return $modal({
                template: 'item-modal.html',
                scope: $scope,                
          });
})
在my item-modal.html中

//other html
<button type="button" class="btn btn-primary" ng-click="confirm()">Click me</button>
//other html
<button type="button" class="btn btn-primary" ng-click="$close(myResult)">Click me</button>
//其他html
点击我

或者使用您自己的方法作为ng click处理程序,该处理程序将调用$scope.$close,其中包含您希望作为承诺结果传回的任何内容。

+1因此,如果我想使用自己的方法而不在my modal.html中传递参数,我可以使用ng click='clickme()'是的,以及clickme()方法调用$close或$DISCLESH关闭模式,并将结果传递回您的孩子。但是,promise返回的结果是整个item-modal.html,而不是方法的myResult。你能帮我澄清一下吗?我尽量避免广播,因为这不是最好的练习。谢谢