gpt4 book ai didi

javascript - ng-show if json 返回 [false] with angularjs

转载 作者:行者123 更新时间:2023-11-28 04:47:43 25 4
gpt4 key购买 nike

我是 Angulars 的新手,我正在做一个项目,我希望 div 仅在 json 数据返回 [false] 时显示。有时,当查询没有结果时,我会让 json 返回 [false]

JS

 .controller('query_ctrl', function($scope, $http){ $http.get('http://localhost/db/templates/search/matches.php').success(function(data){
console.log(JSON.stringify(data));
$scope.matches=data;
});
})

HTML

<div ng-controller="query_ctrl">
<div align="center" ng-show ="$scope.matches='false'">json returned false
</div>

<div align="center" ng-show ="$scope.matches!='false'">json returned true
</div>
</div>

最佳答案

您不需要在 HTML 中引用 $scope。

<div ng-controller="query_ctrl">
<div align="center" ng-show ="matches=='false'">json returned false
</div>

<div align="center" ng-show ="matches!='false'">json returned true
</div>
</div>

此外,正如上面的评论所说 - 我不确定您为什么要将值与字符串“false”进行比较,我假设您想要与 bool 值 false 进行比较。见下文。

<div ng-controller="query_ctrl">
<div align="center" ng-show="matches==false">json returned false
</div>

<div align="center" ng-show ="matches!=false">json returned true
</div>
</div>

您可以通过将匹配作为表达式使 ng-show 表达式更简洁,ng-show 将评估它是否为真值,请参见下文。

<div ng-controller="query_ctrl">
<!-- Will show if matches is a false value -->
<div align="center" ng-show="!matches">json returned false
</div>

<!-- Will show if matches is a truthy value -->
<div align="center" ng-show ="matches">json returned true
</div>
</div>

关于javascript - ng-show if json 返回 [false] with angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40631974/

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