简体   繁体   English

将ID传递给Angular JS中的JSON数据

[英]Passing id to json data in angular js

I am trying to create a chat application through angular and php. 我正在尝试通过angular和php创建一个聊天应用程序。 I have created 2 controllers. 我已经创建了2个控制器。

One is for displaying the number of peoples online and other controllers is for sending and recieving messages. 一种用于显示在线人数,其他控制器用于发送和接收消息。

In MainController I can show the person name whom I clicked but in SendMessageController I don't know how to show the same person name. 在MainController中,我可以显示单击的人的名字,但是在SendMessageController中,我不知道如何显示相同的人的名字。

Please help me, I'm new to angular. 请帮助我,我是新手。 here is working Plunker Link 这里正在工作Plunker Link

Use service to share the data between controllers. 使用服务在控制器之间共享数据。 Inject the service to both controllers. 将服务注入到两个控制器。

app.service('userService', function() {

    this.userId = '';

    this.setUserId = function(id) {
        this.userId = id;
    };

    this.getUserId = function() {
        return this.userId;
    };

}

Full example 完整的例子

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

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