【问题标题】:Why is my ng-style not working?为什么我的 ng-style 不起作用?
【发布时间】:2017-05-29 21:32:04
【问题描述】:

我正在尝试使用ng-style 设置样式。我有一个数组:

scope.order = [0, 1, 2, 3, 4, 5]

我正在尝试使用它在div 上设置订单:

<div class="col-md-3" ng-show="country.usesAddrDistrict" ng-style="{'order': order.indexOf(3)}">
 ...
</div>

并且没有设置顺序。

我试过ng-style="{order: order.indexOf(3)}" 这也行不通。我正在使用 Angular 1.5.8。这是如何工作的?

【问题讨论】:

    标签: angularjs ng-style


    【解决方案1】:

    您的代码看起来不错,您只需使数组在模板上可见,而不是使用块范围声明。

    尝试定义附加到 $scope 的数组。

    $scope.order = [0, 1, 2, 3, 4, 5];
    

    或者如果你使用控制器作为语法

    this.order = [0, 1, 2, 3, 4, 5]
    

    【讨论】:

    【解决方案2】:

    尝试将订单分配给 $scope。

    let order = [0, 1, 2, 3, 4, 5]
      angular.module('myApp', [])
          .controller("example", ["$scope", function (s) {
              s.order = order;
          }])
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <div ng-app="myApp">
      <div class="container" ng-controller="example">
        <div ng-style="{'order': order.indexOf(3)}"> Order:  {{order.indexOf(3)}}</div>
      </div>
    </div>

    【讨论】:

    • 附加到scope。我在复制到 SO 时错过了这一点。我可以让index.orderOf(3) 登录到页面,但它不适用于style
    • 适用于样式标签。你能检查元素并检查吗?
    猜你喜欢
    • 2014-08-31
    • 2016-05-12
    • 2016-02-29
    • 1970-01-01
    • 2014-02-27
    • 2015-08-13
    • 1970-01-01
    • 2016-10-18
    • 2013-08-14
    相关资源
    最近更新 更多