【问题标题】:Populate angular form value using json data使用 json 数据填充角度形式值
【发布时间】:2019-08-20 20:06:14
【问题描述】:

我有 json 响应 {"CustomerAccount":{"segment":"Enterprise","type":"Domestic"}} 这个 json 从另一个 url 获取。 到目前为止我做了什么,这个 json 发送到用 angular 构建的第二个 url,并且已经在控制台上打印了。

我尝试使用 <input type="text" class="form-control form-control-sm" id="nama-ocr" name="{{ segment }}"> 将 json 填充为角度页面中的表单值

有人怎么做吗?我应该在 component.ts 文件中放什么?

还尝试了来自 github 的 dannyvankooten 的 populate.js,但只是在处理非角度 html

谢谢,

【问题讨论】:

    标签: json angular typescript


    【解决方案1】:

    好吧,如果我正确地收集了所有内容,那么您已经将返回的 json 对象记录到控制台。我将假设您将异步返回变量记录到控制台。因此,我将尝试帮助您将该异步对象存储在组件对象中,并将值打印到您的 html 文件中。

    在您的打字稿文件中,您需要导出一个界面,该界面的结构与您希望从您的服务接收的数据相同。在您的模块导入后立即执行此操作:

    ...
    import { Component, OnInit } from '@angular/core';
    ...
    
    export interface CustomerAccountObject {
       CustomerAccount: {
          segment: string;
          type: string;
       }
    }
    

    然后在你的 ts 文件中,声明你的对象:

    export class SomeComponent implements OnInit {
       public customerAccountObject: CustomerAccountObject; // We're defining an object and giving it the Type of the interface we just exported.
       ...
    }
    

    现在,当您通过异步订阅数据时,您将返回的 json 对象设置为您的 customerAccountObject:

    export class SomeComponent implements OnInit {
       public customerAccountObject: CustomerAccountObject;
       ...
    
       constructor(){}
    
       this.yourHttpService.yourMethodOrEndPoint(yourParams).subscribe((result) => {
          this.customerAccountObject = JSON.parse(result);
          console.log(this.customerAccountObject);
       });
    

    现在在 html 端,您可以像这样打印您的值,假设您希望输入 VALUE 采用属性值:

    <input type="text" class="form-control form-control-sm" id="nama-ocr" name="some name" value="{{ customerAccountObject.CustomerAccount.segment }}">
    

    希望这会有所帮助。

    【讨论】:

    • 嗨@kenef,非常感谢您解释它说未定义(对不起,我是新来的角度)。这个到底要放在哪里?正在建设中吗? this.yourHttpService.yourMethodOrEndPoint(yourParams).subscribe((result) =&gt; { this.customerAccountObject = JSON.parse(result); console.log(this.customerAccountObject); });
    • @Bireon 您将该代码放在类的实例化中和构造函数之后。我会更新我的答案。
    • @Bireon 你能在 jsfiddle 或类似的东西中分享你的代码吗?如果我可以查看您的 ts 和 html 文件,我应该能够帮助您使您的组件正常工作。我每天都在 Angular 7 和 8 中工作,并经常开发您的要求。与我分享更多代码,我会修复它,然后添加大量 cmets 解释我做了什么以及为什么这样做。
    • 嗨@kenef,我一定会分享的。你更喜欢 zip 文件还是 stackblitz?
    • 看看你的代码是否不能复制粘贴到codepen中的html、css和js块中。我这里设置了一个环境:codepen.io/Kenef/pen/mdbOqxE 这样我们既可以编辑代码又可以聊天。
    【解决方案2】:

    您可以简单地使用ngModel 绑定它,示例:

    <input type="text" class="form-control form-control-sm" id="nama-ocr" name="segment" [(ngModel)]="myJSON['CustomerAccount']['segment']">
    

    当您的 JSON 被获取或更新时,这将自动更新输入中的值。

    工作样本:https://stackblitz.com/edit/angular-jqh7m6

    【讨论】:

    • 嗨@Yash Krishan,提前感谢会尝试一下。
    • 嗨,我测试并使用了静态数据,如何将动态URI作为json数据?到目前为止,我实际上将 json 数据发送到 url
    • 检查我添加的链接,我已经动态更新了 JSON。
    • 嗨,我检查了链接,但仍然与之前的链接相同
    • “如何将动态URI作为json数据”到底是什么意思?
    猜你喜欢
    • 2015-12-20
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2019-04-06
    • 2016-12-11
    相关资源
    最近更新 更多