根据控制器中的变量显示或隐藏元素 - Ionic

2023-12-24

据我所知,这可能更多的是 AngularJS 问题,而不是 Ionic 特定问题。我的一个视图中有一个按钮:

<button class="button button-clear button-block button-positive" ui-sref="register">
    Register
 </button>

在我的控制器中,我有一个从本地存储获取的变量,该变量是 true 或 false,并且必须根据值隐藏:

app.controller('loginController', ['$scope', '$localstorage',
  function($scope, $localstorage) {

  // Check if the user has already requested a register, and if true, hide
  // the 'Register' button
  if ($localstorage.get("registrationRequested", false) === true) {
    // How do I do this?
  }

}]);

现在第一个问题可能是,像我的控制器那样操纵 dom 是否是最佳实践?如果没有,我该在哪里以及如何做?如果可以在我的控制器中执行此操作,那么我如何引用该按钮并隐藏它?


Add a ng-hide指向您的按钮标签的指令:

<button ng-hide=registered class="button button-clear button-block button-positive" ui-sref="register">
    Register
</button>

在您的 JS 文件中,在您的$scope to false并将其设置为true隐藏按钮:

app.controller('loginController', ['$scope', '$localstorage',
    function($scope, $localstorage) {
        $scope.registered = false;

        // Check if the user has already requested a register, and if true, hide
        // the 'Register' button
        if ($localstorage.get("registrationRequested", false) === true) {
            $scope.registered = true;
        }
    }
]);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据控制器中的变量显示或隐藏元素 - Ionic 的相关文章

随机推荐