gpt4 book ai didi

javascript - 使用ng-if检查angularjs中另一个指令的 Angular 变量

转载 作者:行者123 更新时间:2023-11-29 21:14:10 26 4
gpt4 key购买 nike

我是 Angular 的新手。我的任务是在特定设计中显示 json 数据,但我不知道 json 的嵌套。是否可以检查数据是json还是json数组,然后显示输出。

这里我试图只显示数组数据。但它不起作用。请帮忙。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp">

<div ng-controller="readJson as rj">


<div ng-repeat="d in rj.data">
<ul>
<div ng-model="myVar" ng-init="myVar = "{{isArray(d.third)}}>
<div ng-if="myVar">
<li>{{d.third}}</li>

</div>
</ul>
</div>
</div>


</div>

</body>
<script>
(function(){
var app=angular.module('myApp',[]);

app.controller('readJson',function($scope){
$scope.isArray = angular.isArray;
this.data=[{"first":"one","second":"two","third":"three"},
{"first":"ONE","second":"two","third":"three"},
{"first":"onE","second":"two","third":
[
{"first":"1one","second":"2two","third":"3three"},
{"first":"1ONE","second":"2two","third":"3three"}
]}];

});
})();
</script>

</html>

最佳答案

给你。此示例仅当“third”参数为数组时才显示数据。 https://plnkr.co/edit/EX0BvcYrLPFbiN2ezlqQ

<div ng-repeat="d in data">
<ul>
<div ng-init="myVar = isArray(d.third)"></div>
<div ng-if="myVar">
<li>{{d.third}}</li>
</div>
</ul>
</div>

问题出在 ng-init="myVar = "{{isArray(d.third)}} - 你需要确保 isArray(d.third) 在表达式中(在 ""之间)并且没有必要在 ng-init 指令中使用 {{}} 语法。它将为您评估表达式(查看示例)。

关于javascript - 使用ng-if检查angularjs中另一个指令的 Angular 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40219716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com