简体   繁体   English

如何显示不同的文本 boolean json

[英]how to display a different text boolean json

In my json file I have boolean and I want to display different texts if it's true or false example: if true show 'yes' else show 'no' in html.在我的 json 文件中,我有 boolean,如果是真或假,我想显示不同的文本示例:如果为真,则显示“是”,否则在 ZFC35FDC70D5FC69D269883A822C7A53E 中显示“否”。

I hope I was clear我希望我很清楚

thank you谢谢你

json.file json.file

 {
        "boatType": "Semi-rigide",
        "img": "/assets/img/boat-img/semi-rigide.jpg",
        "longeur": 10,
        "largeur": 20,
        "tirantEau": 50,
        "equipage": false,
        "annexe": true
    },

component.ts组件.ts

export class TableComponent implements OnInit {

  user: IBoat[] = [];

  constructor(private boatService: BoatService) { }

  ngOnInit(): void {
    this.boatService.getBoat()
      .subscribe(data => {
        this.user = data
      });
  }
}

html html

 <tbody>
        <tr *ngFor="let boat of user">
            <td data-label="img"><img [src]="boat.img" alt="boat" class="img"></td>
            <td data-label="longeur">{{boat.longeur}} cm</td>
            <td data-label="largeur">{{boat.largeur}} cm</td>
            <td data-label="tirant d'eau">{{boat.tirantEau}} cm</td>
            <td data-label="equipage">{{boat.equipage}}</td>
            <td data-label="annexe">{{boat.annexe}}</td>
        </tr>
    </tbody>

First create a variable called jsonData in your json file:首先在 json 文件中创建一个名为 jsonData 的变量:

jsonData =  {
        "boatType": "Semi-rigide",
        "img": "/assets/img/boat-img/semi-rigide.jpg",
        "longeur": 10,
        "largeur": 20,
        "tirantEau": 50,
        "equipage": false,
        "annexe": true
    }

Then reference the json file in your script tag in the head然后在头部的脚本标签中引用 json 文件

<head>
<!-- Other code -->
<script src="file.json">
 
</head>

Then parse the json然后解析json

var parsed = JSON.parse(jsonData)

Now you seem a little vague around this part which variable you are checking if true/false so I'll provide code for the two booleans.现在您在这部分似乎有点模糊,您正在检查哪个变量是真/假,所以我将为这两个布尔值提供代码。

parsed.equipage ? console.log("yes") : console.log("No")
// other code
parsed.annexe ? console.log("yes") : console.log("no")

If you wanted to change directly an element's text on the page simply do如果您想直接更改页面上元素的文本,只需执行

document.getElementById("id").innerText = "Yes"

And substitute for console.log() if you wish.如果您愿意,可以替换为console.log()

If I have misinterpreted the question just say Hope this answer helped you:)如果我误解了这个问题,请说希望这个答案对你有帮助:)

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

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