加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:16:54 所属栏目:安全 来源:网络整理
导读:我试图隐藏/显示基于Controller布尔变量的表单的一部分.这是我的 HTML代码: div id="sheetform-container" ng-controller="SheetController as SheetCtrl" form action="#" div class="sheetform-row" ng-show="canShow('Price')" div class="sheetform-lef
我试图隐藏/显示基于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>

我创建了一个函数,根据发送的值将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;
    }
}]);

知道我错过了什么吗?

谢谢!

解决方法

如果您打算将价格作为某物的实际价格,那么在这种情况下您不应该将其用于布尔值.使用ng-model分配价格.另外,不要使用大写字母来命名变量.只有课程应该大写.

<div id="sheetform-container" ng-controller="SheetController as SheetCtrl">
  <form action="#">
    <div class="sheetform-row" ng-show="showPrice">
      <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" ng-model="price" /></div>
      <div class="sheetform-right-col fl-right"></div>
    </div>
  </form>
</div>

然后在您的控制器中,您可以删除您拥有的功能,并初始化变量

ngApp.controller('SheetController',function($scope) {
    $scope.showPrice = true;
    $scope.price = null;

}]);

我不确定你是如何确定价格是否应该显示但你可以将$scope.showPrice分配给一个属性,无论该形式是什么对象,或者如果它是一个切换,那么你可以说:

<a href ng-click="showPrice = !showPrice"></a>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读