【问题标题】:How to get key/value data from observable in angular2如何从 angular2 中的 observable 获取键/值数据
【发布时间】:2018-01-15 04:31:51
【问题描述】:

尝试使用 rxjs 以角度从服务器打印键/值

以下是我的代码

  let listener = c.listenFor('addMessage');
  listener.subscribe(mesg => {

          console.log(mesg);

  });

但它只打印密钥:

FIELD_NAME

KVD_VRT

我想要一些看起来像:

FIELD_NAME:9.02798989742435

KVD_VRT:2.96959280174672

我的 javascript 代码工作正常,但不知道如何在 angular 上做同样的事情:

        chat.client.addMessage = function (name, message) {
            // Html encode display name and message.
            var encodedName = $('111').text(name).html();
            var encodedMsg = $('<div >').text(message).html();
            // Add the message to the page.
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };

不知道如何处理角度处理、订阅键/值数据。

任何帮助都会被占用

【问题讨论】:

    标签: javascript angular typescript angular-cli angular2-template


    【解决方案1】:

    假设你的消息是一个对象。

    你试过了吗

    let listener = c.listenFor('addMessage');
    listener.subscribe(mesg => {
    console.log(mesg.entires());
    

    });

    通常

    Object.entries() 
    

    为数组中的每个条目返回一个键值对数组

    如果你想显示你的键对应的值,那么在你的 Component.html 文件中使用

    <tr *ngFor=" let msg of mesg"> <td>{{msg.FIELD_NAME}} </td> <td> {{msg.KVD_VRT}} </td> </tr>

    【讨论】:

    • 感谢您的回复。但有人抱怨: 'objectconstructor' 类型上不存在 property'entries' 。在我快速谷歌之后,因为我使用 es2016,它需要 es2017 或找到一个 Polyfill。但我还没有弄清楚如何添加一个 polyfill。
    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多