简体   繁体   English

在AngularJS中调用另一个控制器

[英]Call another controller in AngularJS

I have a function in controller 1 that return an array of object, 我在控制器1中有一个函数,该函数返回对象数组,

And in Controller 2, I have to loop in the list that I created so i thought that this will work. 在Controller 2中,我必须循环输入我创建的列表,因此我认为这将起作用。

 $rootScope.$emit("CallParentMethod").forEach(function(row) 

        { 
            console.log(row.key); 

        });

but the object that i'm getting doesn't have the same format that i was expecting , with console.log I saw that object that i'm getting in Controller2 is like 但是我得到的对象与我期望的格式不相同,通过console.log我看到我在Controller2中得到的对象就像

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

So how can I loop into an object that I got from another controller. 因此,如何循环进入从另一个控制器获得的对象。

You can pass objects to events: 您可以将对象传递给事件:

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

Then in your other controller: 然后在另一个控制器中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM