我试图在父 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/