我的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,我将看不到该元素,因为它根本无法访问它。
预期结果 我想显示/隐藏元素,具体取决于isItAvailable的值。
答案 0 :(得分:1)
将其附加到$scope
时,只需像这样使用
<div class="col-lg-8" ng-if="isItAvailable">
</div>
如果要使用ControllerAs语法,请在控制器中将isItAvailable
定义为this
而不是$scope
,然后在视图中命名ng-controller="yourControllerName as Ctrl"
。然后,您将isItAvailable
称为Ctrl.isItAvailable