gpt4 book ai didi

angularjs - 在条件下使用 NaN

转载 作者:行者123 更新时间:2023-12-04 03:13:30 25 4
gpt4 key购买 nike

我的模式中有一个分区,我只需要在变量值 $scope.ornamentweightinbank 时显示分区不等于 NaN .

<div class="rows container-fluid" ng-if="ornamentweightinbank!==NaN">

这是处理NaN的常规方式吗?

最佳答案

可以访问JavaScript的原生isNaN通过在 Controller 中的 this 上创建一个类似的函数来实现功能,然后您可以在 ngIf 中访问它像这样的指令:

(function() {

'use strict';

angular.module('app', []);

})();

(function() {

'use strict';

angular.module('app').controller('MainController', MainController);

MainController.$inject = ['$scope', '$timeout'];

function MainController($scope, $timeout) {

// make isNaN available in your view via controller as syntax
this.isNaN = function(value) {

return isNaN(value);

}

// set ornamentweightinbank to a number
$scope.ornamentweightinbank = 10;

// for demonstrative purposes set ornamentweightinbank to NaN after 5 seconds
$timeout(function() {

$scope.ornamentweightinbank = 10 * "test";

}, 5000);

}

})()
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>

<div ng-app="app" ng-controller="MainController as MainCtrl">

<div class="rows container-fluid" ng-if="!MainCtrl.isNaN(ornamentweightinbank)">
This will show if ornamentweightinbank is not equal to NaN
</div>

<div class="rows container-fluid" ng-if="MainCtrl.isNaN(ornamentweightinbank)">
This will show if ornamentweightinbank is equal to NaN
</div>

</div>

请参阅this stackoverflow answer为什么不能在表达式中直接使用 isNaN

关于angularjs - 在条件下使用 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43017887/

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