Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了显示/隐藏基于Controller布尔变量的AngularJS元素大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图隐藏/显示基于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>

大佬总结

以上是大佬教程为你收集整理的显示/隐藏基于Controller布尔变量的AngularJS元素全部内容,希望文章能够帮你解决显示/隐藏基于Controller布尔变量的AngularJS元素所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。