Angular中,函数返回true,但是ng-show不工作

Angular, function return true but ng-show won't work

本文关键字:ng-show 但是 工作 返回 函数 Angular true      更新时间:2023-09-26

showOrHide函数将返回true如果我点击链接。

我用alert和console.log函数测试了这段代码,它可以工作,但是如果我想把这段代码测试成ng-show或ng-hide指令,它就不能工作了。我不明白这个问题。

Sum:函数返回false,如果我点击某个链接,它返回true但是true和false在ng-show中不起作用。

angular.js文件

$scope.showOrHide = function (value) {
        if (typeof value !== 'undefined')
        {
            return alert(true);
            //console.log("true");
            //return true;
        }else {
            return alert(false);
            //console.log("false");
            //return false;
        }
    };
    $scope.projectId = $stateParams.projectId;
    $scope.showOrHide($stateParams.projectId);

html文件

<tr ng-repeat="n in data.projects track by $index"
    ng-href="@{{n.id}}"
    ui-sref="goTo({ projectId: n.id })">
    <td>@{{n.project_code}}</td>
    <td>@{{n.project_name}}</td>
    <td class="text-primary">will add</td>
    <td class="text-primary">will add</td>
    <td class="text-primary">@{{n.status}}</td>
    <td ng-show="showOrHide">test</td>
</tr>

你的ng-show是基于一个函数而不是一个变量,所以你需要把它写成一个函数。

HTML:

<td ng-show="showOrHide('undefined')">test</td>

JS:

$scope.showOrHide = function (value) {
        if (typeof value !== 'undefined')
            return true;
        else 
            return false;
    };

如果我理解了您想要实现的目的(仅当$stateParams。投影存在)你可以只使用你已经设置的变量,所以删除函数,只使用下面的:

控制器:

app.controller('myController', function( $scope, $stateParams) {
  // other code here...
  $scope.projectId = $stateParams.projectId || false;
});

视图:

<tr ng-repeat="n in data.projects track by $index"
    ng-href="@{{n.id}}"
    ui-sref="goTo({ projectId: n.id })">
    <td>@{{n.project_code}}</td>
    <td>@{{n.project_name}}</td>
    <td class="text-primary">will add</td>
    <td class="text-primary">will add</td>
    <td class="text-primary">@{{n.status}}</td>
    <td ng-show="projectId">test</td>
</tr>

我放弃了所有的解决方案,因为这些都不起作用,所以我没有修改我的代码。我都改了

这就是解决方案。

JS文件

$scope.checkThis = true;
$scope.isShow = function (number) {
    return number;
};
$scope.toFalse = function () {
    $scope.checkThis = false;
};
$scope.toTrue = function () {
    $scope.checkThis = true;
};

HTML文件

<tr ng-repeat="n in data.projects track by $index"
    ng-href="@{{n.id}}"
    ui-sref="goTo({ projectId: n.id })">
    <td>@{{n.project_code}}</td>
    <td>@{{n.project_name}}</td>
    <td class="text-primary">will add</td>
    <td class="text-primary">will add</td>
    <td class="text-primary">@{{n.status}}</td>
    <td class="text-primary" ng-click="toFalse()"
                        ng-show="isShow(checkThis)">test</td>
                    </tr>
我认为这是简单的方法。我浪费了3个小时。

创建如下结果变量:

$scope.toShowOrToHide = $scope.showOrHide($stateParams.projectId);
HTML:

<td ng-show="toShowOrToHide">test</td>

和JS返回没有警告:

$scope.showOrHide = function (value) {
    return (typeof value !== 'undefined');
};

编辑:

$scope.toShowOrToHide = function() {
    return $scope.showOrHide($stateParams.projectId);
);
HTML:

<td ng-show="toShowOrToHide()">test</td>

你可能已经决定了什么最适合你....

First

$scope.stateParams = $stateParams;
然后

ng-show="stateParams.projectId"

不需要函数,参数改变时值自动更新