如何从 Controller 访问 AngularJS 上的 $scope 变量以查看

How to access $scope variables on AngularJS from Controller to view

我的 AngularJS 控制器上有一个简单的范围变量。我已经为它分配了一个特定的端点值,如下所示:

$scope.isItAvailable = endpoint.IS_IT_AVAILABLE;

我如何在我的视图中分配它 (HTML) 以便有一个 ng-if 说,如果它是真的显示它,如果它是假的隐藏它。

我试过它实现一个函数,有一个 ctrl.checkIfavailable,并在 HTML 中调用它,但没有任何帮助。从未在视图端读取该值。

像这样:

  $scope.checkIfItIsAvailable = () => {
    return $scope.isZKAvailable
  }

并在 ng-if 中调用了它。也尝试过作为控制器,但没有用。

我consoled.log服务器的响应,我得到一个布尔值true or false,视情况而定

这是我的 HTML 代码:

<div class="col-lg-8" ng-if="Ctrl.isItAvailable">
.... // More code here
</div>

在控制器中:

  $scope.isItAvailable = endpoint.IS_IT_AVAILABLE;

  console.log(endpoints.IS_IT_AVAILABLE); // This returns the boolean value I 
  // want to access

当前结果 现在,如果我像那样离开 ng-if,我就看不到该元素,因为它根本无法访问它。

预期结果 我想要 show/hide 元素,取决于 isItAvailable 的值。

当你把它附加到 $scope 时,就这样使用它

<div class="col-lg-8" ng-if="isItAvailable">

</div>

如果要使用 ControllerAs 语法,请在控制器中将 isItAvailable 定义为 this 而不是 $scope,然后在视图中命名 ng-controller="yourControllerName as Ctrl"。然后您将 isItAvailable 称为 Ctrl.isItAvailable