调用方法并将值从controller1发送到AngularJS中的controller2 [英] call a method and send value from controller1 to controller2 in AngularJS

查看:50
本文介绍了调用方法并将值从controller1发送到AngularJS中的controller2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此回复链接我尝试过单击按钮并从Controller1到Controller2调用方法时发送数据这是我的尝试:

based on this reply link I tried to send a data when I click on a button and call a method from Controller1 to Controller2 this is my try:

Controller1:

Controller1:

$scope.DetailsLivraison = function(){                                       
                var idv = $scope.idBonSortie;
       $rootScope.$emit("CallParentMethod", idv);                                                                                                  
}

Controller2:

Controller2:

$rootScope.$on("CallParentMethod", function(){
                   $scope.parentmethod(idv);
                });

$scope.parentmethod = function(idv) {
 //Data traitment
}

我的问题是,第二个控制器中的方法未调用,我在两个控制器中都定义了$ rootscope任何帮助请解决问题谢谢你的帮助

my problem is that,the method in the second controller is not called,I have defined $rootscope in both controllers any help please to solve the problem thanks for help

推荐答案

首先,要做到这一点,您的两个控制器都应同时处于活动状态.

第二,您可以使用以下代码:

Secondly, you can use the code below:

$rootScope.$broadcast('CallParentMethod', { //can also use $emit
    idv: idv,
});

在另一个控制器的接收端:

At the receiving end in the other controller:

$rootScope.$on('CallParentMethod', function(event, args) {

       $scope.parentmethod(args.idv);

});

这篇关于调用方法并将值从controller1发送到AngularJS中的controller2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆