Angular,函数 return true 但 ng-show 不起作用

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

如果我单击 link,

showOrHide 函数将 return 为真。

我用 alert 和 console.log 函数测试了这段代码,它可以工作,但如果我想将这段代码测试到 ng-show 或 ng-hide 指令中,它就不会工作。我无法理解这个问题。

Sum: 函数 return false,如果我点击一些 link 它 return 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;

    };

创建结果变量,如:

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

AND HTML:

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

和 JS return 没有警报:

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

已编辑:

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

和HTML:

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

您可能已经决定什么适合您....

如果我理解你想要实现的目标(仅当 $stateParams.projectId 存在时才显示元素)你可以只使用你已经设置的变量,所以 删除函数 并使用以下内容:

控制器:

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.stateParams = $stateParams;

然后

ng-show="stateParams.projectId"

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