显示/隐藏基于Controller布尔变量的AngularJS元素

rel*_*lez 5 angularjs angularjs-controller

我试图隐藏/显示基于Controller布尔变量的表单的一部分.这是我的HTML代码:

<div id="sheetform-container" ng-controller="SheetController as SheetCtrl">
  <form action="#">
    <div class="sheetform-row" ng-show="canShow('Price')">
      <div class="sheetform-left-col fl-left"><label for="sheetform-price">Price</label></div>
      <div class="sheetform-midlle-col fl-left"><input type="text" class="sheetform-input" id="sheetform-price" name="price" value="$ 0.00" /></div>
      <div class="sheetform-right-col fl-right"></div>
    </div>
  </form>
</div>
Run Code Online (Sandbox Code Playgroud)

我创建了一个函数,根据发送的值将Price属性更改为true/false,称为setConfig.这是Controller代码的样子:

ngApp.controller('SheetController', ['$scope', function($scope) {
    $scope.Price = true;

    $scope.canShow = function(field) {
        return $scope.Price;
    }

    $scope.setConfig = function(config) {
        $scope.Price = config.Price;
    }
}]);
Run Code Online (Sandbox Code Playgroud)

知道我错过了什么吗?

谢谢!

ilj*_*ilj 0

你缺少$digest()。Angular 仅在摘要循环中更新 DOM。

官方文档

$watch $apply 如何运行 $digest