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

3 angularjs ng-show ionic-framework ng-hide ionic

据我所知,这可能更像是一个AngularJS问题,而不是一个特定的Ionic问题.我的一个观点中有一个按钮:

<button class="button button-clear button-block button-positive" ui-sref="register">
    Register
 </button>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我有这个变量,我从本地存储得到的是真或假,必须隐藏,具体取决于值:

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?
  }

}]);
Run Code Online (Sandbox Code Playgroud)

现在第一个问题可能是,从控制器那里操纵dom是一种最佳实践吗?如果没有,我在哪里以及如何做到这一点?如果它在我的控制器中做得很好,那我该如何引用该按钮并隐藏它?

Era*_*hel 9

ng-hide在按钮标记中添加指令:

<button ng-hide=registered class="button button-clear button-block button-positive" ui-sref="register">
    Register
</button>
Run Code Online (Sandbox Code Playgroud)

在您的JS文件中,在您的$scopeto中声明此值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;
        }
    }
]);
Run Code Online (Sandbox Code Playgroud)