gpt4 book ai didi

javascript - AngularJS bool 值总是在范围内返回 true

转载 作者:行者123 更新时间:2023-11-29 00:40:06 24 4
gpt4 key购买 nike

所以我有一个需要两个特定参数(subjectID 和 isCalc)的状态:

  .state('tabsController.formulaPicker', {
url: '/my/:subjectID?withCalc',
views: {
'tab1': {
templateUrl: 'templates/formulaPicker.html',
controller: 'formulaPickerCtrl'
}
}
})

& 在我的 Controller 中我有:

  .controller('formulaPickerCtrl', function ($scope, $stateParams, $http) {
$scope.subjectID = $stateParams.subjectID;
$scope.subjectName = '';
$scope.withCalc = $stateParams.withCalc;
$scope.formulas = [];

$http.get('../Data.json')
.success(function (data, status, headers, config) {
// Alerts correct value
alert($scope.withCalc);
// Always returns true
$scope.subjectSet = ($scope.withCalc) ? data.subjects : data.subjects_no_calc;
$scope.subjectName = $scope.subjectSet[$scope.subjectID].name;
$scope.formulas = $scope.subjectSet[$scope.subjectID].formulas;
})
.error(function (data, status, headers, config) {
})
.then(function (result) {
});
})

但是在我声明范围的 subjectSet 值的行中,我有一个三元运算符来检查 isCalc bool 值,它从我请求的 json 文件。每次我加载 Controller 时,它都会提醒 isCalc 值,这是我想要的并且是正确的。但是三元表达式总是返回 true,即使它警告为 false。这导致我返回错误的值集。

我从第一个 Controller 中获取了两个 ui-sref 属性:

ui-sref="tabsController.formulaPicker({subjectID: $index, withCalc: true})"

对于第一部分&

“tabsController.formulaPicker({subjectID: $index, withCalc: false})”

第二部分。

请记住它会提示正确的值,但无论如何三元表达式总是返回 true。

提前谢谢你。

最佳答案

检查$scope.withCals是否不是字符串对象。您可能需要将其显式转换为 bool 值。

关于javascript - AngularJS bool 值总是在范围内返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39715594/

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