调用 AngularJS 中的另一个控制器



我在控制器 1 中有一个返回对象数组的函数,

在控制器 2 中,我必须在我创建的列表中循环,所以我认为这会起作用。

 $rootScope.$emit("CallParentMethod").forEach(function(row) 
        { 
            console.log(row.key); 
        });

但是我得到的对象与我期望的格式不同,带有控制台.log 我看到了我在控制器2中得到的那个对象就像

  {name: "CallParentMethod", targetScope: l, defaultPrevented: false, currentScope: null}

那么我怎样才能循环到我从另一个控制器获得的对象中。

您可以将对象传递给事件:

function letSomethingHappen() {
    $rootScope.$broadcast("CallParentMethod", {
        title: "Let's pass this string!"
    });
}

然后在您的另一个控制器中:

$rootScope.$on("CallParentMethod", function(event, passedArgs) {
    console.log(passedArgs.title); // Let's pass this string!
});

最新更新